【问题标题】:Read JSON Array in Python在 Python 中读取 JSON 数组
【发布时间】:2020-02-14 19:54:42
【问题描述】:

我正在尝试在 python 中读取部门数组。我可以读取数组的第一个元素。我如何阅读其余部分,例如“aaa”、“bbb”、“ccc”的值?有什么建议吗?

import json


person2 = '{"FirstName":"2018 Q1 (JAN-MAR)","LastName":"aaaaaaaaaaaaaaaaaaaaaaaa","Phone":"2222222222222222","Department":[{"cpt":"A0001","aaa":"1","bbb":"2","ccc":"3","ddd":"4","eee":"5"},{"cpt":"A0002","aaa":"6","bbb":"7","ccc":"8","ddd":"9","eee":"10"},{"cpt":"A0003","aaa":"11","bbb":"21","ccc":"31","ddd":"44","eee":"55"},{"cpt":"A0004","aaa":"101","bbb":"22","ccc":"33","ddd":"44","eee":"55"},{"cpt":"A0005","ffs":"100","mmc":"23","med":"34","oth":"45","duo":"56"},{"cpt":"A0006","aaa":"111","bbb":"24","ccc":"34","ddd":"44","eee":"54"}],"checkAgree":true}'
person_dict = json.loads(person2)


#Example of how to read JSON array elements in python
print(person_dict['Department'][0]["cpt"]);

print(person_dict['Department'][1]["cpt"]);

print(person_dict['Department'][3]["cpt"]);

print(person_dict['Department'][4]["cpt"]);

print(person_dict['Department'][5]["cpt"]);
"Department": [
    {
      "cpt": "A0001",
      "aaa": "1",
      "bbb": "2",
      "ccc": "3",
      "ddd": "4",
      "eee": "5"
    },
    {
      "cpt": "A0002",
      "aaa": "6",
      "bbb": "7",
      "ccc": "8",
      "ddd": "9",
      "eee": "10"
    },
    {
      "cpt": "A0003",
      "aaa": "11",
      "bbb": "21",
      "ccc": "31",
      "ddd": "44",
      "eee": "55"
    },
    {
      "cpt": "A0004",
      "aaa": "101",
      "bbb": "22",
      "ccc": "33",
      "ddd": "44",
      "eee": "55"
    },
    {
      "cpt": "A0005",
      "aaa": "100",
      "bbb": "23",
      "ccc": "34",
      "ddd": "45",
      "eee": "56"
    },
    {
      "cpt": "A0006",
      "aaa": "111",
      "bbb": "24",
      "ccc": "34",
      "ddd": "44",
      "eee": "54"
    }
  ],

【问题讨论】:

  • 如果您尝试读取Department 中除cpt 之外的其他元素会怎样? print(person_dict['Department'][0]["aaa"]);print(person_dict['Department'][0]["bbb"]);
  • 您尝试访问这些字段的原因是什么?出了什么问题?
  • 我忘记尝试了! @chrisbyte

标签: python arrays json


【解决方案1】:

您只需更改要查找的元素的名称

print(person_dict['Department'][0]["aaa"])
print(person_dict['Department'][0]["bbb"])
print(person_dict['Department'][0]["ccc"])

等等

您也可以根据您要实现的目标在循环中执行此操作,仅作为示例

def get_attr(elem, attr):
  if attr in elem:
    return elem[attr]
  return ''

for e in person_dict['Department']:
  print('%s %s %s %s' % (get_attr(e, 'cpt'), get_attr(e, 'aaa'), get_attr(e, 'bbb'), get_attr(e, 'ccc')))

输出:

A0001 1 2 3
A0002 6 7 8
A0003 11 21 31
A0004 101 22 33
A0005
A0006 111 24 34

【讨论】:

    【解决方案2】:

    如果您想重复代码,您只需传递key。假设您有一本包含列表和该列表中的字典的字典,您不应该以任何不同的方式对待它:

    for i in range(len(person_dict['Department'])):
       for k in person_dict['Department'][i]:
           print("Key =",k,"Value =",person_dict['Department'][i][k])
    

    这将输出:

    Key = cpt Value = A0001
    Key = aaa Value = 1
    Key = bbb Value = 2
    Key = ccc Value = 3
    Key = ddd Value = 4
    Key = eee Value = 5
    

    ...等等

    【讨论】:

      【解决方案3】:

      只需遍历所有元素:

      for d in person_dict['Department']:
          for k,v in d.items():
              print("Key =", k ,"Value =", v)
      
      

      【讨论】:

        猜你喜欢
        • 2021-11-05
        • 2011-05-13
        • 2012-05-11
        • 2016-02-02
        • 2018-02-23
        • 1970-01-01
        • 2022-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多