从JSON文件刮擦数据
我正在尝试从JSON文件刮擦 name ,但它们向我显示字符串索引必须是整数
,如您所见 JSON文件:
"prodBean": {
"subProdTerms": null,
"friendlyUrl": "/brakes-wheel-end/brake-drums-rotors/brake-drum/otr-brake-drum-otr1601b",
"name": "OTR 16.5 x 7 Brake Drum, Balanced",
"showSubscriptionSelection": false,
"UnitOfMeasure": "EA",
"qtyPerUnit": 1.0,
"sequence": 2,
"availMsg": null,
"ProductStatus": "Released",
"ProductType": "Product",
"sku": "OTR-OTR1601B",
"id": "a8G1W000000Y7DfUAK",
"showNewSubscriptionSelection": false,
"savings": 405.49,
"basePrice": 405.492,
"price": 0.0,
"ECrossReferencesS"
代码
import json
with open("test.json","r") as file:
temp=json.load(file)
for item in temp:
title=item['prodBean']['name']
print(title)
I am trying to scrape name
from json file but they show me error that string indices must be integers
as you see below json code
Json file:
"prodBean": {
"subProdTerms": null,
"friendlyUrl": "/brakes-wheel-end/brake-drums-rotors/brake-drum/otr-brake-drum-otr1601b",
"name": "OTR 16.5 x 7 Brake Drum, Balanced",
"showSubscriptionSelection": false,
"UnitOfMeasure": "EA",
"qtyPerUnit": 1.0,
"sequence": 2,
"availMsg": null,
"ProductStatus": "Released",
"ProductType": "Product",
"sku": "OTR-OTR1601B",
"id": "a8G1W000000Y7DfUAK",
"showNewSubscriptionSelection": false,
"savings": 405.49,
"basePrice": 405.492,
"price": 0.0,
"ECrossReferencesS"
Code
import json
with open("test.json","r") as file:
temp=json.load(file)
for item in temp:
title=item['prodBean']['name']
print(title)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,加载语句返回
dict
。当您用单个参数迭代dict时,您只会得到键:您需要使用
.items()
迭代dict,那么您将可以访问数据:The load statement in your case returns a
dict
. When you iterate a dict with a single parameter, you get just the keys:You need to iterate the dict with
.items()
, then you'll have access to the data: