django - 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
。
解决方案
试试这个e
:bnm = json_string.get('c').get('d').get('e')
或列表:
bnm = json_string.get('c')[0].get('d').get('e')
推荐阅读
- .net - NuGet 包:将 dll 和配置文件放在子目录下
- xslt - XSLT 将数字添加到子字符串
- python - 在 PyQT5 中使用信号和插槽时,有没有办法从不同的 python 文件调用函数?
- android - 在不同的国家推出不同的版本
- php - 如何在php中为数组编写foreach循环
- javascript - 如何在 Javascript 中使用变量创建 JSON?
- ios - UIUserInterfaceStyle 键在 Xcode 12 的 Info.plist 中不可用,如何从 Info.plist 禁用 Xcode 12 中的暗模式?
- c++ - 通过将值从 int 更改为 float,总值会发生变化 谁能解释为什么?请
- react-testing-library - 如何组合复杂的选择器
- powershell - 无法从 Internet 加载自定义 PowerShell 脚本