简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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