我正在尝试从API解码数据,只是想不出一种干净的方法来提取值和时间值。我一直在尝试进行字符串操作,但最终变得非常复杂。
{"max_scale": "0", "min_scale": "0", "graph_label": "Light Level", "average": "1", "length_of_time": "3600", "upper_warn": "1000", "lower_warn": "30", "cached": false, "values":
[{"value": 0.0, "time": 1531170219},
{"value": 0.0, "time": 1531170159},
{"value": 0.0, "time": 1531170099},
{"value": 0.0, "time": 1531170039},
{"value": 0.0, "time": 1531169979},
{"value": 0.0, "time": 1531169919},
{"value": 0.0, "time": 1531169859},
{"value": 0.0, "time": 1531169799},
{"value": 0.0, "time": 1531169739},
{"value": 0.0, "time": 1531169679},
{"value": 0.0, "time": 1531169619},
{"value": 0.0, "time": 1531166679}],
"timestamp_to": "1531170222.798", "format_string": "%f Lux"}
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以为此使用Python的json
模块,或者您可以自己修改数据并将其转换为dict,因为实际上它已经是一个了。然后,您需要做的就是像这样遍历它:
data = {"max_scale": "0", "min_scale": "0", "graph_label":
"Light Level", "average": "1", "length_of_time": "3600",
"upper_warn": "1000", "lower_warn": "30", "cached": 'false',
"values":
[{"value": 0.0, "time": 1531170219},
{"value": 0.0, "time": 1531170159},
{"value": 0.0, "time": 1531170099},
{"value": 0.0, "time": 1531170039},
{"value": 0.0, "time": 1531169979},
{"value": 0.0, "time": 1531169919},
{"value": 0.0, "time": 1531169859},
{"value": 0.0, "time": 1531169799},
{"value": 0.0, "time": 1531169739},
{"value": 0.0, "time": 1531169679},
{"value": 0.0, "time": 1531169619},
{"value": 0.0, "time": 1531166679}],
"timestamp_to": "1531170222.798", "format_string": "%f Lux"}
for item in data['values']:
time, value = item['time'], item['value']