簡體   English   中英

如何在 python 中獲取數組 json 的鍵和值

[英]How to get key and value of array json in python

我有一個示例數組 json:

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

預期:我想通過 PyThon 獲取每個數組,其中包含名稱和所有數組元素,例如“衣服”和“連衣裙”是一個數組的名稱

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

請幫我解決這個問題。 非常感謝和愛你!!!!

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)

Output -

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM