Python 从包含列表的json中提取值作为键的值

Python 从包含列表的json中提取值作为键的值,python,json,rest,Python,Json,Rest,我目前正在做一个数据科学项目(初学者),有以下场景: 我有一个带有Pincode、地址和城市的数据框(大约57000行) 我需要同样的地理坐标 我正在尝试使用Bing Map API获取python中的坐标。但我一直在解析Json响应 pincodelist=[] import json i=0 for i in range(5): #just trying with first 5 rows countryRegion = "IN" locality = df_all.

我目前正在做一个数据科学项目(初学者),有以下场景:

  • 我有一个带有Pincode、地址和城市的数据框(大约57000行)
  • 我需要同样的地理坐标
  • 我正在尝试使用Bing Map API获取python中的坐标。但我一直在解析Json响应

    pincodelist=[]
    
    import json
    
    i=0
    
    for i in range(5): #just trying with first 5 rows
    
        countryRegion = "IN"
        locality = df_all.iloc[13,6] #references the location column
        postalCode =df_all.iloc[13,12] #references the pincode column
        addressLine = df_all.iloc[13,0] #references the address
        BingMapsKey = 'my api key'
    
        url="http://dev.virtualearth.net/REST/v1/Locations?countryRegion="+str(countryRegion)+"&locality="+str(locality)+"&postalCode=" + str(postalCode)+"&addressLine="+str(addressLine)+"&key="+str(BingMapsKey)
         # make the GET request
        results = requests.get(url).json()
    
        pincodelist.append([
                        addressLine, 
                        postalCode,
                        results(['resourceSets']['resources']['bbox'])])
    print(pincodelist)
    
    我得到以下错误:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-198-396207c04bc6> in <module>
         20                     addressLine,
         21                     postalCode,
    ---> 22                     results(['resourceSets']['resources']['bbox'])])
         23 print(pincodelist)
         24 
    
    TypeError: list indices must be integers or slices, not str
    

    如果考虑到要查询的行数,您可以参考任何其他位置数据服务,这也会很有帮助。作为一名学生,付费服务对我来说是不可行的。

    resourceSets
    是一个对象列表,可以通过键后面的方括号看到,因此您需要指定一个数字索引以从中获取元素。
    资源
    键也是如此

    {'resourceSets': [{'estimatedTotal': 1, 'resources': [{'__type': ...
    
    在您的示例中,只有一个
    资源集
    ,因此我们可以只获取第一个元素:

    #资源集-列表
    资源集合=结果['resourceSets']
    #资源集对象
    资源集合=资源集合[0]
    #资源-列表
    资源=资源集['resources']
    #第一资源
    资源=资源[0]
    #bbox-一个包含4个数字的列表
    bbox=资源['bbox']
    #或者在一行中:
    结果['resourceSets'][0]['resources'][0]['bbox']
    
    使用json.Parse将响应解析为json

    我试着用javaScript实现,并将json响应分配给变量t

    如果能够使用此引用进行提取,则某些键将列表作为其值,这可能是您面临的问题

    t.resourceSets[0].resources[0].bbox
    (4) [12.91842713075696, 77.56459359208381, 12.926152565898313, 77.57516165693963]
    

    发布有效且完整的json,以及您希望从json中提取哪些值?@komatiraju032:我已经用一个查询的实际json输出编辑了描述(没有运行for循环)。我需要的是“bbox”下面列出的坐标,非常感谢您的提示。我忽略了dict包含一个列表作为值的事实。
    t.resourceSets[0].resources[0].bbox
    (4) [12.91842713075696, 77.56459359208381, 12.926152565898313, 77.57516165693963]