Python JSON TypeError 列表索引必须是整数或切片,而不是 str
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44591062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python JSON TypeError list indices must be integers or slices, not str
提问by Matthewj
I am currently trying to parse some data from a post request response and I keep getting this error: "TypeError: list indices must be integers or slices, not str"
我目前正在尝试从发布请求响应中解析一些数据,但我不断收到此错误:“类型错误:列表索引必须是整数或切片,而不是 str”
Python Code
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 Response
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"
}
}
}
}
I did a similar piece of code but it was a get request and everything turned out fine.
我做了一段类似的代码,但它是一个 get 请求,结果一切都很好。
回答by depperm
data['result']['results']
is an array so you can't do ['name']
you need an int, you could add [0]
after['results']
and it should work. Then you can reference keys within the object in results
.
data['result']['results']
是一个数组,所以你不能['name']
需要一个int,你可以在[0]
后面添加['results']
它应该可以工作。然后您可以在results
.
回答by Ngoc Hoang Nguyen
In my case:
就我而言:
[ { "_index": "abc", "_type": "abc", "_score": null, "_source": { "layers": { "frame_raw": [ "frame": { ...... "raw": [
Correct way to access "raw" values is
访问“原始”值的正确方法是
data = json.load(json_file) data[0]['_source']['layers']['raw'][0] data[0]['_source']['layers']['raw'][1] ...
In case of multiple data:
如果有多个数据:
[ { "_index": "abc", "_type": "abc", "_score": null, "_source": { "layers": { "frame_raw": [ "frame": { ...... "raw": [ { "_index": "abc", "_type": "abc", "_score": null, "_source": { "layers": { "frame_raw": [ "frame": { ...... "raw": [
Get it by:
通过以下方式获取:
data = json.load(json_file) for d in data: print(d['_source']['layers']['raw'][0])