我有一个json文件,正在读取并尝试使用json.load()
进行解析,但是遇到困难
遇到数组对象。我有以下json对象,需要遍历SrvTcp
的元素
我个人不确定,该怎么做:
{
"SrvTcp": [
{
"max_dst_port": 111,
"min_dst_port": 112,
"name": "Resource",
"uid": "16ffd8b1-3555-4f30-912c-7777"
},
{
"max_dst_port": 65535,
"name": "Resource Server Reply",
"uid": "hhd-0361-4b01-aacf-a"
}
]
}
我尝试了以下方法:
with open("outputfile", 'w') as f:
with open(jsonFile) as normalJson: #json file from above
data = json.load(normalJson)
for key in data:
f.write(key) # 'SrvTcp' gets written
f.write(json.dumps(data[key]) #entire array(two elements) gets written
f.write(data[key]) #nothing gets written
f.write(len(data)) #nothing gets written
f.write(data[key][0]) # nothing gets written. I would like the first element of SrvTcp
如何提取SrvTcp数组的每个元素,以便可以分别编写每个元素?