Thingspeak:用Python解析json响应

2024-10-06 11:35:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个使用Python的Alexa技能来使用传感器上传到Thingspeak的数据。在我只使用一个特定值的情况下是非常容易的,Thingspeak的响应只是值。当我想使用几个值来总结大气压力来确定趋势时,响应是一个json对象,如下所示:

{"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"}]

我现在从

^{pr2}$

json对象有点递归,它是一个列表,其中包含一个以数组为值的元素。 现在我不太确定如何迭代数组中键“field2”的值。我对Python和json非常陌生。也许谁能帮我?在

提前谢谢!在


Tags: 数据对象idjson技能情况传感器数组
3条回答

我刚想好了,一切如期而至。 您必须从dict中获取entries数组,然后遍历项目列表并将值打印到键field2。在

# Get entries from the response
entries = json_object["feeds"]

# Iterate through each measurement and print value
for entry in entries:
  print entry['field2']

这与json无关—一旦json.load()解析了json字符串,您得到的是一个普通的python对象(通常是dict,有时是列表,但这是合法的字符串、int、float、boolean或None)。在

it is a list containing a list with an element with an array as the value.

实际上它是一个dict,有两个键"channel"和{}。第一个有另一个dict for value,第二个是dicts的list。如何使用dict和list在FWIW中有大量的文档

这里,您要查找的值存储在“feeds”键中dicts的“field2”键下,因此您需要的是:

# 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"]

你有一本字典。使用键访问值

例如:

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"]

输出:

^{pr2}$

相关问题 更多 >