使用python

时间:2015-06-10 13:10:16

标签: python json

我从谷歌api附近的一个位置,我得到了json,当我尝试拆分json我收到错误

{  
  u'status':u'OK',
  u'next_page_token':u'CoQC9AAAAHepdLFIvAUuqz6y6WWasKBmq5aAOYr0Bbu97upCMy4EI1Ea5t-6S6iObZdZ5_RIB7ywocdG-lF9ian5JRuTQVGL7MwbBa_uN3EfS7XzjmlVx-IKsauiEiO-Wu3r25zk9SL3yc5d_vDGvN3VQJkA7bBiDWhkloJ4RFngjBsGVWVQOnj5glrbwVVrw9Nu6DNi70C2Wdqqy_65b_jFjJiJYTAwrlfoyl7GGpxk5Gng7QgSFdtTJII9zdfkxcj3osUzklRetjraDtgfaQgxr0KA_H5btbuXz3UT6r-dyqdj2qd1tr_0oAvFkGB9t0qFbUYSe7bDETEAwdDv7MSmmXeYHQUSEMCBruHU5pb8X4EoPbPw9ncaFLgqTTICkQyGYY-boaJ1_3X3SaeT',
  u'html_attributions':[  
  u'Listings by <a href="http://www.indiacom.com/">Indiacom Yellow Pages</a>'],
  u'results':[  
  {  
     u'name':u'Institute for Financial Management and Research',
     u'reference':u'CpQBgwAAAL5Gg4T18LzUpNTEzvKWeAH0jLBuTyC_rmxOycL3KndgQ05WVKovVhiIYhnnqeOxcX1tcWesIi0vSVwugaskyy2UnJ_BrTD5ZblXzD7nLxP9L-FOQLetRgbpA6DlNzHM6Nmcu3jtJiBAOyMQJOmgL9cot7c4y18o_3E1cJrzPJfg5hK6trq2u2lvJnD2ZxJ6IxIQC2IuHwQILkrbtUd3ke5GDBoU1sZLoPY-_kARc7lEoq2naKHtwSk',
     u'geometry':{  
        u'location':{  
           u'lat':13.062882,
           u'lng':80.238669
        }
     },
     u'place_id':u'ChIJKzE7o2ZmUjoRLaCtNPjba3U',
     u'vicinity':u'24,
     Kothari Road,
     Nungambakkam,
     Chennai',
     u'photos':[  
        {  
           u'photo_reference':u'CnRoAAAApH-YJpJFjPYltZYhYTs_tIVFA7vve-LMii8XbUydZJLMXbzDNkxuCuGCk9W-nFjgUrj-JoRqJLRuurGvt1oz94osENNc8bZGLBI4Joj1w-dQSyiwqqzqDdna-u0TRkJ_8S91fF3uerww341951YB2hIQX7gFjIn5tWkkEcGwErJ9oBoU0CdKRd6b2pL3Bcp09hCYvleEfaQ',
           u'width':816,
           u'html_attributions':[  
              u'From a Google User'
           ],
           u'height':459
        }
     ],
     u'scope':u'GOOGLE',
     u'id':u'2e9a63cf7368e0f90e2a20711ac56853b7c34462',
     u'types':[  
        u'school',
        u'establishment'
     ],
     u'icon':         u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png'
  },
  {  
     u'rating':4.2,
     u'name':u'Sri Sankara Senior Secondary School',
     u'reference':u'CoQBdgAAAJ-Uc78EbPnLX6adzheZMWrS9sOJ9vWTQsqZOlQza-r3qozDUrl4XxWPRdHD9K_BVP0t_FhEwQt4w42X0z01uQr7dtq5cZ7ioa9zBVIQpwOkSQhxjbjQjX05YxVqGPB9MCfEikHpFKSKIaz5mPrLDgklbhQ8clD4fm9BiWNmE_mJEhD35R4GgbVNu4J-x0Lfaw3BGhRPQEXErZf3jJJkLbHs2HWVRvP2Xg',
     u'geometry':{  
        u'location':{  
           u'lat':13.009931,
           u'lng':80.260746
        }
     },
     u'place_id':u'ChIJh_fXcelnUjoRd4vKDQfY_DM',
     u'vicinity':u'9/21 Vasantha Press Road,
     Vasanta Press Road,
     Adyar,
     Landmarks are Malar Hospital/Theosophical society,
     Chennai',
     u'photos':[  
        {  
           u'photo_reference':u'CnRwAAAAIrFQSUJn7JB5_GgDfEPBldHptKmARqhV-6HR5fUT-MjB6ScO7ZYz1jamqoGvTqXlbEZZjxC67BvOllBHTiRIQwKyBXoI9DhleBmrCgMTrorjeDkvIDY_8ZC0pOFZOZGGH2XdfLrH1irsWZUEa0IjFRIQaATxA2BymP1KED4vxNZfnxoUTwD5Y-4-8ZPnPrhuKofUVSztcoQ',
           u'width':297,
           u'html_attributions':[  

           ],
           u'height':297
        }
     ],
     u'scope':u'GOOGLE',
     u'id':u'a8dc412bac3ea790260d2c7d6fe08271ae883a4e',
     u'types':[  
        u'school',
        u'establishment'
     ],
     u'icon':         u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png'
  },
  {  
     u'rating':4.5,
     u'name':u'Chettinad Vidyashram',
     u'reference':u'CnRoAAAAbIG1-6ecTuOcqw5hCenhtbHlAmP-nfdw_W1vEv94fXvIyCzhHSQMn95VEtKCgbLeME6qd30uGhxmLxFwXItcls-SlC7fgXwGl2JINCLTjB1RYpYC--Gr6hS-9cT7Xq2f46-dAqnpF5n2sRa1cNJJSBIQvVLDztqmh2BmqkJER9MLZxoU3gbS1TpgVj8h5Uo71QKTTyj1CdQ',
     u'geometry':{  
        u'location':{  
           u'lat':13.017083,
           u'lng':80.269503
        }
     },
     u'place_id':u'ChIJR3w9SdxnUjoRs2vfnH-ERNA',
     u'vicinity':u'Rajah Annamalaipuram,
     Chennai',
     u'photos':[  
        {  
           u'photo_reference':u'CnRoAAAAeUHwPDKO87eeGP7Fzm7aKE3VcQd6gFebbjo2FhYRHdulLZW-XdepstzETly74Id6NMOF5lqm4BHZ56C1CRnsxmdqaxJ-rcJR2Cpq2VfJaixZmBG3C-0TTNmMuPuGsjKAldr6rWCWdDVMg8FAnWhgyRIQXYPX89XdA5fl7e5RUecRWhoU-SExDqUr-GRaYVLkb8Iq_1mf-R8',
           u'width':968,
           u'html_attributions':[  
              u'From a Google User'
           ],
           u'height':968
        }
     ],
     u'scope':u'GOOGLE',
     u'id':u'f3b774d4c11a4bd20585669d9c4ae57fc12e5652',
     u'types':[  
        u'school',
        u'establishment'
     ],
     u'icon':         u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png'
  },

这是我的python代码

res = json.dumps(response)
  for result in response[status][results]:
    print result['status']

因为json很大我只放了半个json数据。 我得到的错误是1431年:全球名称&#39;状态&#39;未定义

如何分割这个json

当我打印类型(响应)时

  

输入元组

1 个答案:

答案 0 :(得分:0)

如果你得到一个json字符串,你想用loads将它加载到本机结构中,然后迭代该原生结构。查看json字符串,您看起来好像个别结果没有状态字段。你可以这样做:

res = json.loads(response)
print res['status']
for result in res['results']:
    print result