Thingspeak:用Python解析json响应
我想创建一个Alexa技能,使用Python使用传感器上传到Thingspeak的数据。我只使用一个特定值的情况非常简单,Thingspeak的响应是唯一的值。当我想使用几个值时,在我的例子中,为了总结大气压力来确定趋势,响应是一个json对象,如下所示:Thingspeak:用Python解析json响应,python,json,parsing,Python,Json,Parsing,我想创建一个Alexa技能,使用Python使用传感器上传到Thingspeak的数据。我只使用一个特定值的情况非常简单,Thingspeak的响应是唯一的值。当我想使用几个值时,在我的例子中,为了总结大气压力来确定趋势,响应是一个json对象,如下所示: {"channel":{"id":293367,"name":"Weather Station","description":"My first attempt to build a weather station based on an E
{"channel":{"id":293367,"name":"Weather Station","description":"My first attempt to build a weather station based on an ESP8266 and some common sensors.","latitude":"51.473509","longitude":"7.355569","field1":"humidity","field2":"pressure","field3":"lux","field4":"rssi","field5":"temp","field6":"uv","field7":"voltage","field8":"radiation","created_at":"2017-06-25T07:35:37Z","updated_at":"2018-08-04T12:11:22Z","elevation":"121","last_entry_id":1812},"feeds":
[{"created_at":"2018-10-21T18:11:45Z","entry_id":1713,"field2":"1025.62"},
{"created_at":"2018-10-21T18:12:05Z","entry_id":1714,"field2":"1025.58"},
{"created_at":"2018-10-21T18:12:25Z","entry_id":1715,"field2":"1025.56"},
{"created_at":"2018-10-21T18:12:45Z","entry_id":1716,"field2":"1025.65"},
{"created_at":"2018-10-21T18:13:05Z","entry_id":1717,"field2":"1025.58"},
{"created_at":"2018-10-21T18:13:25Z","entry_id":1718,"field2":"1025.63"}]
我现在从
f = urllib.urlopen(link) # Get your data
json_object = json.load(f)
for entry in json_object[0]
print entry["field2"]
json对象是一个有点递归的对象,它是一个列表,包含一个列表,其中一个元素的值是数组。
现在我不太确定如何迭代数组中键“field2”的值。我对Python和json都很陌生。也许有人能帮我
提前谢谢 你有一本字典。使用键访问该值 Ex:
json_object = {"channel":{"id":293367,"name":"Weather Station","description":"My first attempt to build a weather station based on an ESP8266 and some common sensors.","latitude":"51.473509","longitude":"7.355569","field1":"humidity","field2":"pressure","field3":"lux","field4":"rssi","field5":"temp","field6":"uv","field7":"voltage","field8":"radiation","created_at":"2017-06-25T07:35:37Z","updated_at":"2018-08-04T12:11:22Z","elevation":"121","last_entry_id":1812},"feeds":
[{"created_at":"2018-10-21T18:11:45Z","entry_id":1713,"field2":"1025.62"},
{"created_at":"2018-10-21T18:12:05Z","entry_id":1714,"field2":"1025.58"},
{"created_at":"2018-10-21T18:12:25Z","entry_id":1715,"field2":"1025.56"},
{"created_at":"2018-10-21T18:12:45Z","entry_id":1716,"field2":"1025.65"},
{"created_at":"2018-10-21T18:13:05Z","entry_id":1717,"field2":"1025.58"},
{"created_at":"2018-10-21T18:13:25Z","entry_id":1718,"field2":"1025.63"}]}
for entry in json_object["feeds"]:
print entry["field2"]
1025.62
1025.58
1025.56
1025.65
1025.58
1025.63
输出:
json_object = {"channel":{"id":293367,"name":"Weather Station","description":"My first attempt to build a weather station based on an ESP8266 and some common sensors.","latitude":"51.473509","longitude":"7.355569","field1":"humidity","field2":"pressure","field3":"lux","field4":"rssi","field5":"temp","field6":"uv","field7":"voltage","field8":"radiation","created_at":"2017-06-25T07:35:37Z","updated_at":"2018-08-04T12:11:22Z","elevation":"121","last_entry_id":1812},"feeds":
[{"created_at":"2018-10-21T18:11:45Z","entry_id":1713,"field2":"1025.62"},
{"created_at":"2018-10-21T18:12:05Z","entry_id":1714,"field2":"1025.58"},
{"created_at":"2018-10-21T18:12:25Z","entry_id":1715,"field2":"1025.56"},
{"created_at":"2018-10-21T18:12:45Z","entry_id":1716,"field2":"1025.65"},
{"created_at":"2018-10-21T18:13:05Z","entry_id":1717,"field2":"1025.58"},
{"created_at":"2018-10-21T18:13:25Z","entry_id":1718,"field2":"1025.63"}]}
for entry in json_object["feeds"]:
print entry["field2"]
1025.62
1025.58
1025.56
1025.65
1025.58
1025.63
这与json无关-一旦json字符串被
json.load()
解析,您将得到一个普通的python对象(通常是dict,有时是list,很少-但这是合法的-字符串、int、float、boolean或None
)
它是一个列表,包含一个列表,列表中的元素的值为数组
实际上,它是一个带有两个键的dict
“频道”和“提要”
。第一个有另一个dict表示值,第二个是dict
s的list
。FWIW广泛记录了如何使用dicts和list
# get the list stored under the "feeds" key
feeds = json_object["feeds"]
# iterate over the list:
for feed in feeds:
# get the value for the "field2" key
print feed["field2"]
我刚想出来,就像预料的那样。 您必须从dict中获取
条目
数组,然后迭代项目列表并将值打印到键字段2
# Get entries from the response
entries = json_object["feeds"]
# Iterate through each measurement and print value
for entry in entries:
print entry['field2']
谢谢你的解释,我只是自己想出来的。这就是它的工作原理。:-)谢谢你的回答,这和预期的一样。