python - 如何从 json 值中抓取属性
问题描述
我试图通过一个看起来像这样的 json 来抓取一些值:
{
"attributes":{
"531":{
"id":"531",
"code":"taille",
"label":"taille",
"options":[
{
"id":"30",
"label":"40",
"is_in":"0"
},
{
"id":"31",
"label":"41",
"is_in":"1"
}
]
}
},
"template":"Helloworld"
}
我的问题是,我试图抓取的每个 json 文件中的数字 531 都不同,而我试图通过这个 json 抓取的是标签和is_in值
到目前为止,我所做的是我试图做这样的事情,但我被卡住了,不知道如果 531 换成别的东西该怎么办
getOption = '{
"attributes":{
"531":{
"id":"531",
"code":"taille",
"label":"taille",
"options":[
{
"id":"30",
"label":"40",
"is_in":"0"
},
{
"id":"31",
"label":"41",
"is_in":"1"
}
]
}
},
"template":"Helloworld"
}'
for att, values in getOption.items():
print(values)
那么我怎样才能刮取价值label
和is_in
?
解决方案
我不确定您是否可以拥有多个 531 键,但您可以遍历它们。
getOption = {
"attributes":{
"531":{
"id":"531",
"code":"taille",
"label":"taille",
"options":[
{
"id":"30",
"label":"40",
"is_in":"0"
},
{
"id":"31",
"label":"41",
"is_in":"1"
}
]
}
},
"template":"Helloworld"
}
attributes = getOption['attributes']
for key in attributes.keys():
for item in attributes[key]['options']:
print(item['label'], item['is_in'])
推荐阅读
- omnet++ - (stage=0) 在 Veins 应用程序中是什么意思?
- tinymce - 如何将模板插件上的下拉列表替换为tinymce 5中的按钮?
- angular - rxjs 在效果器中做
- c++ - MFC ODBC RFX_Binary 在绑定的 CByteArray 中设置伪 Null 值,该值包含在对 DB 的序列化中
- ios - 在swift中将base 64字符串转换为图像返回nil
- c# - 回滚有时不起作用
- twitter-bootstrap - 当导航栏处于移动大小时,Bootstrap 4/5 不同的导航栏品牌 css
- proxy - 带有 DietPi 发行版的覆盆子上 Tor 代理的 Iptables
- php - 使用 YII 框架验证数据库中存储的 JSON 文件中的数据
- javascript - 如何在不使用视频标签的情况下显示用户的网络摄像头视频