json结构
{
"categories": [{
"supercategory": "Bottle",
"id": 1,
"name": "Bottle"
},
{
"supercategory": "Car",
"id": 2,
"name": "Car"
}]
}
Run Code Online (Sandbox Code Playgroud)
由以下python脚本读取:
with open('file.json') as json_data:
json_info = json.load(json_data)
Run Code Online (Sandbox Code Playgroud)
稍后,同一脚本尝试以下列方式访问数据结构:
json_info['1']['name']
json_info['2']['name']
Run Code Online (Sandbox Code Playgroud)
数字指的是json结构中的"id"字段.由于该代码显然与json结构不一致:我如何更改json结构以使其工作?(假设我无法更改脚本).
为了让你的代码工作,你需要这样的东西:
json_info = {
"1": {"supercategory": "Bottle",
"name": "Bottle"},
"2": {"supercategory": "Car",
"name": "Car"}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |