[英]How to format Json query results
#!/usr/bin/env python
import urllib2
import json
api_key = 'VtxgIC2UnhfUmXe_pBksov7-lguAQMZD'
url = 'http://www.energyhive.com/mobile_proxy/getCurrentValuesSummary?token='+api_key
response = urllib2.urlopen(url)
content = response.read()
for x in json.loads(content):
if x["cid"] == "PWER":
print (x["data"])
for y in json.loads(content):
if y["cid"] == "PWER_GAC":
print(y["data"])
for z in json.loads(content):
if z["cid"] == "PWER_IMM":
print(z["data"])
for a in json.loads(content):
if a["cid"] == "FBAK_IMM":
print(a["data"])
When I run the code I get results in this format 当我运行代码时,我会得到这种格式的结果
[{u'1439193214000': 2880}]
[{u'1439193214000': 2979}]
[{u'1439193212000': 3276}]
[{u'1439193212000': 135}]
How do I remove all and just print only the numbers after the : 我如何删除所有内容,而只在以下数字后打印:
json.loads turns it into a python structure, in this case a list of dictionaries, so just call the values function json.loads将其转换为python结构,在这种情况下为字典列表,因此只需调用values函数
import urllib2
import json
api_key = 'VtxgIC2UnhfUmXe_pBksov7-lguAQMZD'
url = 'http://www.energyhive.com/mobile_proxy/getCurrentValuesSummary?token='+api_key
response = urllib2.urlopen(url)
content = response.read()
for x in json.loads(content):
if x["cid"] == "PWER":
print (x["data"][0].values()[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.