python - 如何在python中拆分这种类型的字典
问题描述
我已经使用烧瓶创建了 Web 服务,因此添加了请求 JSON 参数,但现在问题是如何将我的 dict 拆分为简单数据
我的字典是
order={
"userid":17,
"details":[
{
"productid":1,
"eachprice":45,
"quantity":1,
"price":45
},
{
"productid":3,
"eachprice":749,
"quantity":1,
"price":749
}
]
}
所以我分裂喜欢
for x, y in order.items():
print(x, y)
但问题是我如何打印嵌套数据
解决方案
一个快速但不太好(=不是一般)的解决方案是:
for x, y in order.items():
print(x,y, type(y))
if type(y) == list: # check if the value is a list
for element in y: # loop lists
for k, v in element.items(): # loop dictionaries within the list
print(k,v)
推荐阅读
- printjs - 用 printJS 覆盖 css
- sqlite - Vuejs 通过 axios 从 Sqlite3 拉取数据库;DOM 不重新渲染
- amazon-web-services - 跟踪 JupyterHub 中的文件下载活动?
- ios - 自定义 UITableViewCell 自动布局不起作用
- ios - prepareForInterfaceBuilder 中的约束?
- c - 当没有子字符串时,K&R 的几乎相似的 strindex 实现找到了子字符串
- .net - 如何将 textBox 转换为 int,然后在 Visual Studio 2019 中使用它进行简单的数学运算?
- python - 浏览器中的python动画
- jquery - 旁边的可链接图标
- c# - 如何让程序知道一个点是在一条线的右边还是左边?