开发者

accessing elements in simplejson.load() output

i have a dictionary(?) of data returned from the simplejson.load() function. it looks like this...

{'status': 'OK', 'results': [{'geometry': {'location_type': 'APPROXIMATE', 'bounds': {'northeast': {'lat': 53.86121, 'lng': -2.045072}, 'southwest': {'lat': 53.80570600000001, 'lng': -2.162588}}, 'viewport': {'northeast': {'lat': 53.8697753, 'lng': -2.0725853}, 'southwest': {'lat': 53.81711019999999, 'lng': -2.2006447}}, 'location': {'lat': 53.84345099999999, 'lng': -2.136615}}, 'address_components': [{'long_name': 'Trawden', 'types': ['sublocality', 'political'], 'short_name': 'Trawden'}, {'long_name': 'Colne', 'types': ['locality', 'political'], 'short_name': 'Colne'}, {'long_name': 'Lancashire', 'types': ['administrative_area_level_2', 'political'], 'short_name': 'Lancs'}, {'long_name': 'United Kingdom', 'types': ['country', 'political'], 'short_name': 'GB'}], 'formatted_address': 'Trawden, Colne, Lancashire, UK', 'types': ['sublocality', 'political']}]}

How do I get at e.g. results->geometry->location->lat ?

Is this structure a regular python dictionary?

EDIT: please could someone also explain the simplejson.dumps() function. I don't find the docs very enlightening.

thanks

Edit by non-OP: here's the JSON, pretty-printed:

{
  "status":"OK",
  "results":[
    {
      "geometry":{
        "location":{
          "lat":53.843450999999988,
          "lng":-2.1366149999999999
        },
        "location_type":"APPROXIMATE",
        "viewport":{
          "northeast":{
            "lat":53.869775300000001,
            "lng":-2.0725853000000001
          },
          "southwest":{
            "lat":53.817110199999988,
            "lng":-2.2006446999999998
          }
        },
        "bounds":{
          "northeast":{
            "lat":53.86121,
            "lng":-2.0450719999999998
          },
          "southwest":{
            "lat":53.805706000000008,
            "lng":-2.162588
          }
        }
      },
      "address_components":[
        {
          "long_name":"Trawden",
          "short_name":"Trawden",
          "types":[
            "sublocality",
            "political"
          ]
        },
        {
          "long_name":"Colne",
          "short_name":"Colne",
          "types":[
            "locality",
            "political"
          ]
        },
        {
          "long_name":"Lancashire",
          "short_name":"Lancs",
          "type开发者_如何学JAVAs":[
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name":"United Kingdom",
          "short_name":"GB",
          "types":[
            "country",
            "political"
          ]
        }
      ],
      "formatted_address":"Trawden, Colne, Lancashire, UK",
      "types":[
        "sublocality",
        "political"
      ]
    }
  ]
}


Yes, it is. If you store it in a variable named d, then you would use...

d['results'][0]['geometry']['location']

et cetera. Notice the [0] there due to the fact that the dict with key 'geometry' is inside a list.

simplejson.load() maps JSON objects to Python dicts and JSON lists to lists. Very straightforward; don't overthink it.


simplejson.dumps() simply does the opposite of simplejson.loads() - it takes any standard Python object, and dumps it to a string which is a JSON representation of that object. For instance:

>>> q = {}
>>> q['foo'] = 'bar'
>>> q[1] = 'baz'
>>> simplejson.dumps(q)
'{"1": "baz", "foo": "bar"}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜