提问人:darklord84 提问时间:11/15/2023 更新时间:11/15/2023 访问量:14
使用 python 从 json 文件中的键获取值 [已关闭]
Getting values from keys in a json file using python [closed]
问:
我有一个文件test.json,其中包含以下内容:
{
"idCount": "14",
"id": "XXXX1111",
"name": "GRMART-111",
"type": "SHOP",
"items": [
{
"ID": "SLWA1112",
"RFID": "2DHERJHER231J",
"name": "Clothes"
},
{
"clothId": "DR12121",
"clothCount": "2",
"name": "Dress"
},
{
"clothId": "JN121213",
"clothCount": "20",
"name": "Jeans"
},
{
"clothId": "SH123223",
"cothCount": "67",
"name": "Shirt"
},
{
"clothId": "PA132323",
"clothCount": "97",
"name": "Pants"
},
{
"clothId": "BO123121",
"clothCount": "73",
"name": "Boots"
}
]
}
我想提取所有“clothId”的值。输出应如下所示:
[DR12121,JN121213,SH123223,PA132323,BO123121]
我尝试使用以下程序解决此问题
import json
results=[]
f = open('./test.json')
data = json.load(f)
results = [x.get('clothId', None) for x in data]
print(results)
这是我收到的错误:
results = [x.get('clothId', None) for x in data]
AttributeError: 'str' object has no attribute 'get'
你能告诉我我做错了什么以及如何解决这个问题吗?
答: 暂无答案
评论