[英]python json dict iterate {key: value} are the same
i have a json file I am reading in; 我有一个我正在阅读的json文件; looks similar to: 看起来类似于:
[
{
"Destination_IP": "8.8.4.4",
"ID": 0,
"Packet": 105277
},
{
"Destination_IP": "9.9.4.4",
"ID": 0,
"Packet": 105278
}
]
when i parse the json via: 当我解析json时:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {0} | value: {0}".format(key, value))
I am getting: 我正进入(状态:
key: Destination_IP | value: Destination_IP
I have tried using .items()
and I have tried just iterating over the keys via iterkeys()
and keys()
to no avail. 我尝试过使用.items()
,我试过通过iterkeys()
和keys()
迭代按键无效。
I can call it direct via json_dict['Destination_IP']
and the value returns. 我可以通过json_dict['Destination_IP']
直接调用它,然后返回值。
for json_dict in data:
if 'Destination_IP' in json_dict.keys():
print json_dict['Destination_IP']
returns: 收益:
key: Destination_IP | value: 8.8.4.4
I'm on python 2.7, so any help in running down the value portion would be greatly appreciated. 我在python 2.7上,所以在运行值部分的任何帮助都将非常感激。
Change your string formats index : 更改字符串格式索引 :
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {0} | value: {1}".format(key, value))
Or without using index: 或者不使用索引:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {} | value: {}".format(key, value))
Also you can using names instead of index: 您也可以使用名称而不是索引:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {key} | value: {value}".format(key=key, value=value))
You don't need to specify an index at all: 您根本不需要指定索引:
for key, value in json_dict.iteritems():
print("key: {} | value: {}".format(key, value))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.