[英]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.