繁体   English   中英

如何格式化Json查询结果

[英]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"])

当我运行代码时,我会得到这种格式的结果

[{u'1439193214000': 2880}]
[{u'1439193214000': 2979}]
[{u'1439193212000': 3276}]
[{u'1439193212000': 135}]

我如何删除所有内容,而只在以下数字后打印:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM