我正在研究一个terrarium控制器项目,此时我想得到照明的时间段。 在python中我有一个名为:getStatus()的函数,它将JSON字符串返回给Javascript,它将进一步处理数据。 但它不起作用。
这是我的python代码:
def getStatus (): data=[["HOUR_ON", HOUR_ON],["MINUTE_ON", MINUTE_ON], ["HOUR_OFF", HOUR_OFF], ["MINUTE_OFF", MINUTE_OFF]] return json.dumps(data)并且在Javascript上面:
var alertFunction = function macroCallback(macro, args, light) { alert(macro + " returned with " + light); var obj = JSON.parse(light); document.getElementById("testSection").innerHTML="Light turns on at: "+ obj[0].HOUR_ON+":"+obj[0].MINUTE_ON + "\n"+"Light goes out at: "+obj[1].HOUR_OFF+":"+obj[1].MINUTE_OFF; alert("after parsing: "+obj[0]); document.getElementById("testSection").innerHTML=light; }字符串似乎是从服务器发回并将被正确解析,只访问字符串不起作用我在innerHTML部分中得到四次未定义。
我希望有人可以帮助我!
问候和感谢!
马尔科
I am working on a terrarium controller project, at this moment I want to get the timeperiod of the lighting. In python I have a function called: getStatus() it returns a JSON string to Javascript, which will handle the data further. But it doesn't work.
Here's my python code:
def getStatus (): data=[["HOUR_ON", HOUR_ON],["MINUTE_ON", MINUTE_ON], ["HOUR_OFF", HOUR_OFF], ["MINUTE_OFF", MINUTE_OFF]] return json.dumps(data)And overhere the Javascript:
var alertFunction = function macroCallback(macro, args, light) { alert(macro + " returned with " + light); var obj = JSON.parse(light); document.getElementById("testSection").innerHTML="Light turns on at: "+ obj[0].HOUR_ON+":"+obj[0].MINUTE_ON + "\n"+"Light goes out at: "+obj[1].HOUR_OFF+":"+obj[1].MINUTE_OFF; alert("after parsing: "+obj[0]); document.getElementById("testSection").innerHTML=light; }The string seems to be send back from the server and will be parsed correctly, only accessing the string doesn't work I get four times undefined in the innerHTML section.
I hope someone can help me out!
Greetings and thanks!
Marco
最满意答案
在不更改Python代码的情况下,更改您的javascript代码以使用索引访问数组的元素,而不是使用属性:
var alertFunction = function macroCallback(macro, args, light) { alert(macro + " returned with " + light); var obj = JSON.parse(light); document.getElementById("testSection").innerHTML="Light turns on at: "+ obj[0][1]+":"+obj[1][1] + "\n"+"Light goes out at: "+obj[2][1]+":"+obj[3][1]; alert("after parsing: "+obj[0]); document.getElementById("testSection").innerHTML=light; }但是,最好遵循jcubic的建议并更改Python代码以使用字典:
def getStatus (): data={"HOUR_ON": HOUR_ON, "MINUTE_ON": MINUTE_ON, "HOUR_OFF": HOUR_OFF, "MINUTE_OFF": MINUTE_OFF} return json.dumps(data)例如,这会产生一个像这样的JSON字符串:
'{"HOUR_OFF": 13, "MINUTE_ON": 30, "MINUTE_OFF": 20, "HOUR_ON": 12}'同时更改您的javascript以访问对象的属性,这更具可读性:
var alertFunction = function macroCallback(macro, args, light) { alert(macro + " returned with " + light); var obj = JSON.parse(light); document.getElementById("testSection").innerHTML="Light turns on at: "+ obj.HOUR_ON + ":" + obj.MINUTE_ON + "\n" + "Light goes out at: "+obj.HOUR_OFF+":"+obj.MINUTE_OFF; alert("after parsing: "+obj[0]); document.getElementById("testSection").innerHTML=light; }Without changing your Python code, alter your javascript code to access the elements of the array using indices, not with attributes:
var alertFunction = function macroCallback(macro, args, light) { alert(macro + " returned with " + light); var obj = JSON.parse(light); document.getElementById("testSection").innerHTML="Light turns on at: "+ obj[0][1]+":"+obj[1][1] + "\n"+"Light goes out at: "+obj[2][1]+":"+obj[3][1]; alert("after parsing: "+obj[0]); document.getElementById("testSection").innerHTML=light; }However, it would be better to follow jcubic's advice and change your Python code to use a dictionary:
def getStatus (): data={"HOUR_ON": HOUR_ON, "MINUTE_ON": MINUTE_ON, "HOUR_OFF": HOUR_OFF, "MINUTE_OFF": MINUTE_OFF} return json.dumps(data)For example this produces a JSON string like this:
'{"HOUR_OFF": 13, "MINUTE_ON": 30, "MINUTE_OFF": 20, "HOUR_ON": 12}'Also change your javascript to access the attributes of the object, which is much more readable:
var alertFunction = function macroCallback(macro, args, light) { alert(macro + " returned with " + light); var obj = JSON.parse(light); document.getElementById("testSection").innerHTML="Light turns on at: "+ obj.HOUR_ON + ":" + obj.MINUTE_ON + "\n" + "Light goes out at: "+obj.HOUR_OFF+":"+obj.MINUTE_OFF; alert("after parsing: "+obj[0]); document.getElementById("testSection").innerHTML=light; }更多推荐
发布评论