赞
踩
Been banging my head on desk!
I have an url that is downloaded in json, then parsed_json = json.loads(response_body) to python dictionary. The issue is that data(embedded in dicts is in list) that is needed is in a dictionary called list. If I do a dict count or key or anything I just get the first level of 3 when there is actually 37 dicts .
for key, value in parsed_json.iteritems() :
print key, value
After a week I cannot figure out how to get all values from dicts in the list, I can get one by using something like this but I cannot increment the count.
print parsed_json['list'][(1)]['value']
This is a digi xbee cloud get, after getting the values there needs to be some math done on some, as the results are in MV's not C degrees.
Any direction to get the values greatly appreciated.
Snippet of data:
{u'count': 37,
u'list': [{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/management/connections',
u'id': u'00000000-00000000-00409DFF-FF818A13/management/connections',
u'server_timestamp': u'2015-11-21T04:21:45.407Z',
u'timestamp': u'2015-11-21T04:21:45.269Z',
u'type': u'JSON',
u'value': u'{"connectTime":"2015-11-21T04:21:45.269Z","type":"Ethernet","remoteIp":"72.38.16.255","localIp":"192.168.2.106","session":"17c6e5d2-3d9d-439b-aa8a-36f050bf8b9c"}'},
{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD1',
u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD1',
u'server_timestamp': u'2015-11-22T08:14:15.945Z',
u'timestamp': u'2015-11-22T08:14:09.101Z',
u'type': u'INTEGER',
u'value': u'1'},
{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD2',
u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD2',
u'server_timestamp': u'2015-11-22T08:14:15.964Z',
u'timestamp': u'2015-11-22T08:14:09.377Z',
u'type': u'INTEGER',
u'value': u'613'},
{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD3',
u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:0A]!/AD3',
u'server_timestamp': u'2015-11-22T08:14:15.930Z',
u'timestamp': u'2015-11-22T08:14:08.854Z',
u'type': u'INTEGER',
u'value': u'852'},
{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:50]!/AD1',
u'id': u'00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:50]!/AD1',
u'server_timestamp': u'2015-11-22T08:14:15.834Z',
u'timestamp': u'2015-11-22T08:14:07.477Z',
u'type': u'INTEGER',
u'value': u'0'},
{u'history_uri': u'/ws/v1/streams/history/00000000-00000000-00409DFF-FF818A13/xbee.analog/[00:13:A2:00:40:D5:8F:50]!/AD2',
解决方案
This should work:
for index in range(parsed_json['count']):
print(parsed_json['list'][index]['value'])
Or simpler:
for item in parsed_json['list']:
print(item['value'])
You can print all key-value pairs by iterating over items()
for entry in parsed_json['list']:
for key, value in entry.items():
print(key)
print(' ', value)
In Python 2 write print ' ', value because print is a function in Python 3 but still a statement in Python 2. If you are new to Python, start with Python 3. Python 2 is the legacy Python.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。