我想创建一个使用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非常陌生。也许谁能帮我?在
提前谢谢!在
我刚想好了,一切如期而至。 您必须从dict中获取
entries
数组,然后遍历项目列表并将值打印到键field2
。在这与json无关—一旦
json.load()
解析了json字符串,您得到的是一个普通的python对象(通常是dict,有时是列表,但这是合法的字符串、int、float、boolean或None
)。在实际上它是一个}。第一个有另一个dict for value,第二个是
dict
,有两个键"channel"
和{dict
s的list
。如何使用dict和list在FWIW中有大量的文档这里,您要查找的值存储在“feeds”键中dicts的“field2”键下,因此您需要的是:
你有一本字典。使用键访问值
例如:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐