如何在python中获取数组json的键和值

2024-05-23 22:43:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个示例数组json:

[
  {
   'Clothes':[
               {
                 'id': '32705111', 
                 'describes': 'no problem'
               }
             ]
  },
  {
   'Dress':[
               {
                 'id': '32705111', 
                 'describes': 'no outfit'
               }
           ]
  }
]

预期:我希望通过PyThon获得每个数组的名称和所有数组元素,例如'Cloth'和'Dress'是一个数组的名称

Clothes[{'id': '32705111','describes': 'no problem'}], Dress[{'id': '32705111', 'describes': 'no outfit'}]

请帮我解决这个问题。非常感谢你,我爱你


Tags: no名称idjson元素示例数组problem
2条回答
json_arr = [
  {
   'Clothes': [
               {
                 'id': '32705111',
                 'describes': 'no problem'
               }
             ]
  },
  {
   'Dress': [
               {
                 'id': '32705111',
                 'describes': 'no outfit'
               }
           ]
  }
]

for d in json_arr:
    for name, array in d.items():
        globals()[name] = array

试试下面的代码,我想这就是你要找的

new_lst = []

for i in json_array:
    for name, array in i.items():
       value = ''.join(str(array))
       result = name+value
       new_lst.append(result)

print(new_lst)

输出-

["Clothes[{'id': '32705111', 'describes': 'no problem'}]", "Dress[{'id': '32705111', 'describes': 'no outfit'}]"]

相关问题 更多 >