从Python读取JSON对象导致TypeError

从Python读取JSON对象导致TypeError,python,json,Python,Json,我试图从Python中读取JSON。以下是从geocode请求返回的JSON对象: { "results" : [ { "address_components" : [ { "long_name" : "Race Course Lane", "short_name" : "Race Course Ln", "types" : [ "route" ]

我试图从Python中读取JSON。以下是从geocode请求返回的JSON对象:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Race Course Lane",
               "short_name" : "Race Course Ln",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Little India",
               "short_name" : "Little India",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Race Course Ln, Singapore",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 1.3103311,
                  "lng" : 103.85354
               },
               "southwest" : {
                  "lat" : 1.3091323,
                  "lng" : 103.8523656
               }
            },
            "location" : {
               "lat" : 1.3097033,
               "lng" : 103.8529918
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.311080680291502,
                  "lng" : 103.8543017802915
               },
               "southwest" : {
                  "lat" : 1.308382719708498,
                  "lng" : 103.8516038197085
               }
            }
         },
         "place_id" : "ChIJe5XzBMcZ2jERclOJt-xVp_o",
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}
我试图在几何图形下获得格式化的_地址和lat lng。这是我的密码:

json_data = requests.get(url).json()

formatted_address = json_data['results'][0]['formatted_address']
print(formatted_address)
for each in json_data['results'][0]['geometry']:
    print(each['lat'])
我设法打印出格式化的地址,但收到以下错误消息:

Traceback (most recent call last):
File "D:\Desktop\test.py", line 16, in <module>
print(each['lat'])
回溯(最近一次呼叫最后一次):
文件“D:\Desktop\test.py”,第16行,在
打印(每个['lat'])
TypeError:字符串索引必须是整数


有什么想法吗?谢谢

json\u数据['results'][0]['geometry']
是一个
\u指令。这意味着,json_数据['results'][0]['geometry']
中的x的
将导致对键的迭代,其中
x
是一个分配了键(字符串)的循环变量。这里有一个例子-

d = {'a' : 'b', 'c' : 'd'}

for each in d:
     print(x)   

a
c
由于
each
是一个字符串,
each['lat']
将是一个无效的操作(因为您不能用整数以外的任何内容对字符串进行索引)


观察JSON文件的结构-

{
    "location_type": "GEOMETRIC_CENTER",
    ...

    "location": {
        "lng": 103.8529918,
        "lat": 1.3097033
    }
}
仔细观察,只有
位置
具有与之相关联的
lat
lng
键的dict。如果您只需要这些值,那么只需直接为它们编制索引。不需要循环

x = json_data['results'][0]['geometry']['location']
lat, lng = x['lat'], x['lng']

对于d['results'][0]['geometry']
中的x['results'].
导致对键的迭代。而且,由于键是字符串,
str[str]
将出错。您可能需要的是:
x=json_数据['results'][0]['geometry'];现在,对于x:x[each]['lat']
中的每一个,这将导致另一个错误,因为并非所有的键值对都有带lat键的dict。请再看一眼JSON,找出要从表单中提取坐标的位置<代码>位置?我想提取新加坡Ln赛马场,1.3097033103.8529918。lat液化天然气是在location@COLDSPEED嘿,不过跟你简单核对一下。我一直收到一些地址的错误消息:indexer:list索引超出范围。例如,对于地址A,当我第一次运行时,它会向我抛出此错误消息。然后我又跑了第二次,它还给了我lat lng。然后我第三次运行它,我再次收到错误消息。这可能是因为在响应返回之前,我对每个地址的迭代速度太快了吗?@hyperfkcb不,这永远不会发生。响应总是在迭代开始之前收到。我的建议是使用try-except块来捕获错误,并在这种情况下打印出响应,以查看哪里出了问题。