python如何读取字典的关键字_python在列表中获取字典值

1586010002-jmsa.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值