【发布时间】:2017-07-12 04:08:27
【问题描述】:
我是 python 新手,我想了解如何在不参考其名称的情况下访问 json 对象中的数组。
给定的 json 对象具有以下结构
import json
input_json = {
"records": [
{
"values": {
"col1": "1"
},
"no": 1,
},
{
"values": {
"col1": "2"
},
"no": 2,
}
],
"number_of_records": 2
}
myVar = json.load(input_json)
for i in myVar['records']: # How do I replace this line?
print i['values']['col1']
我需要遍历“记录”数组中的对象。如何在不使用 myVar['records'] 的情况下获取数组?
请注意,代码也不能依赖于 json 属性的顺序。唯一可以保证的是 json 字符串中只有一个数组。
【问题讨论】: