Thingspeak:使用Python解析json响应

时间:2018-10-22 11:12:33

标签: python json parsing

我想使用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"}]

我现在开始

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很陌生。也许有人可以帮我吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

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

  

它是一个列表,其中包含一个列表,该列表的元素以数组为值。

实际上是一个dict,带有两个键"channel""feeds"。第一个具有另一个值的字典,第二个具有listdict。 FWIW广泛记录了如何使用字典和列表

这里,您要查找的值存储在“ feeds”键中的字典的“ 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"]

答案 1 :(得分:0)

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

例如:

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

答案 2 :(得分:0)

我刚刚弄清楚了,就像预期的那样。 您必须从字典中获取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']