python提取json数据索引必须是整数_Python JSON TypeError列表索引必须是整数或切片,而不是str...

在尝试从POST请求响应中提取JSON数据时,遇到了"TypeError:列表索引必须是整数或切片,而不是str"的错误。问题出现在尝试访问`data['result']['results']['name']`时。JSON响应包含产品信息,如名称、评级、图片URL等。之前的类似GET请求没有出现此类问题。
摘要由CSDN通过智能技术生成

我目前正在尝试从发布请求响应中解析一些数据,并且不断收到此错误:“ TypeError:列表索引必须是整数或切片,而不是str”

Python代码

import requests

import json

count = 0

params = {'var1':'40', 'value':'143', 'itm':'1', 'param':'1'}

req = 'https://www.api.com/api/search'

data = requests.post(req, data = params).json()

print (data['result']['results']['name'])

JSON回应

{

"result":{

"count":1,

"totalCount":1,

"offset":0,

"queryTime":232,

"results":[

{

"rating":"4.0",

"productId":{

"upc":"143",

"ItemId":"143",

"productId":"143-prd"

},

"name":"Product",

"catagory":{

"name":"",

"CataId":1

},

"images":{

"thumbnailUrl":"http://api.com/img/static/product-image-50-50.png",

"largeUrl":"http://api.com/img/static/product-image-500-500.png"

},

"price":{

"price":13,

"isRealTime":true,

"currencyUnit":"USD"

},

"location":{

"unit":[],

"detailed":[]

},

"inventory":{

"quantity":1,

"status":"In Stock",

"isRealTime":true

},

"ratings":{

"rating":"3.1875",

"ratingUrl":"http://api.com/3_1875.gif"

},

"reviews":{

"reviewCount":"2"

},

"isItem":true,

"lUrl":"/l/Product-Name"

}

],

"performance":{

"enrichment":{

}

},

"query":{

"originalQuery":"143",

"actualQuery":"143",

"suggestedQueries":[

]

},

"algo":"jarvis",

"blacklist":false,

"cluster":{

"apiserver":{

"hostname":"site.api.com",

"pluginVersion":"1.0"

},

"searchengine":{

"hostname":"srch.site.api.com"

}

}

}

}

我做了类似的代码,但这是一个get请求,一切都很好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值