【问题标题】:Python JSON TypeError list indices must be integers or slices, not strPython JSON TypeError 列表索引必须是整数或切片,而不是 str
【发布时间】:2017-11-19 08:07:04
【问题描述】:

我目前正在尝试从发布请求响应中解析一些数据,但我不断收到此错误:“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"
         }
      }
   }
}

我做了一段类似的代码,但它是一个获取请求,结果一切正常。

【问题讨论】:

  • data['result']['results'] 是一个数组,所以你不能做['name'] 你需要一个int,你可以在['name'] 之前添加[0] 它应该可以工作
  • 工作得很好,谢谢!
  • 除了添加[0],您还可以遍历结果,以防有多个结果。

标签: python json


【解决方案1】:

就我而言:

[ { "_index": "abc", "_type": "abc", “_score”:空, “_资源”: { “层”:{ “frame_raw”:[ “框架”: { …… “生的”: [

访问“原始”值的正确方法是

数据 = json.load(json_file) 数据[0]['_source']['layers']['raw'][0] 数据[0]['_source']['layers']['raw'][1] ...

如果有多个数据:

[ { "_index": "abc", "_type": "abc", “_score”:空, “_资源”: { “层”:{ “frame_raw”:[ “框架”: { …… “生的”: [ { "_index": "abc", "_type": "abc", “_score”:空, “_资源”: { “层”:{ “frame_raw”:[ “框架”: { …… “生的”: [

通过以下方式获取:

数据 = json.load(json_file) 对于数据中的 d: 打印(d['_source']['layers']['raw'][0])

【讨论】:

    【解决方案2】:

    data['result']['results'] 是一个数组,所以你不能做['name'] 你需要一个int,你可以在['results'] 之后添加[0] 它应该可以工作。然后您可以在results 中引用对象内的键。

    【讨论】:

    • [0] 应该保留在['results'] 之后,因为那是一个列表
    • 方括号([和])描述了 JSON 列表的开始和结束。列表需要数字索引。
    猜你喜欢
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2020-11-21
    • 2017-10-08
    • 1970-01-01
    • 2015-12-09
    • 2021-11-28
    相关资源
    最近更新 更多