从json文件中提取数组元素

时间:2019-06-27 18:28:21

标签: python arrays python-3.x read-write

我有一个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数组的每个元素,以便可以分别编写每个元素?

0 个答案:

没有答案