首页 > 解决方案 > Django json 嵌套列表 .get()

问题描述

我被一个愚蠢的问题困住了。

我有一个 json 数据并试图将其保存在我的模型中。

这是代码。

response = response.json() #this gives json data
response = json.loads(response) #loads string to json           
json_string = response #ready to get data from list
modelfielda = json_string.get("abc") # this works fine
modelfieldb = json_string.get('["c"]["d"]["e"]') #this does not give data though data is present.

我的 json 数据是这样的:

{  
   "abc":"AP003",
   "c":[  
      {  
         "d":{  
            "e":"some data",
            "f":"some data"
         }
      }
   ]
}

所以我的问题是如何在里面获取数据c

标签: djangodjango-views

解决方案


试试这个e
bnm = json_string.get('c').get('d').get('e')
或列表:
bnm = json_string.get('c')[0].get('d').get('e')


推荐阅读