Python JSON TypeError列表索引必须是整数或切片,而不是str [英] Python JSON TypeError list indices must be integers or slices, not str

查看:144
本文介绍了Python JSON TypeError列表索引必须是整数或切片,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

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请求,结果一切正常.

I did a similar piece of code but it was a get request and everything turned out fine.

推荐答案

data['result']['results']是一个数组,因此您无法执行['name'],而需要一个整数,可以在['results']之后添加[0],它应该工作.然后,您可以在results中的对象内引用键.

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.

这篇关于Python JSON TypeError列表索引必须是整数或切片,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆