访问 simplejson.load() 输出中的元素

发布于 2024-12-04 09:34:56 字数 2710 浏览 1 评论 0原文

我有一个从 simplejson.load() 函数返回的数据的字典(?)。它看起来像这样...

{'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']}]}

我如何获得例如结果->几何->位置->纬度?

这个结构是一个普通的Python字典吗?

编辑:请有人也解释 simplejson.dumps() 函数。我觉得这些文档不太有启发性。

谢谢

非OP编辑:这是JSON,打印得很漂亮:

{
  "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",
          "types":[
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name":"United Kingdom",
          "short_name":"GB",
          "types":[
            "country",
            "political"
          ]
        }
      ],
      "formatted_address":"Trawden, Colne, Lancashire, UK",
      "types":[
        "sublocality",
        "political"
      ]
    }
  ]
}

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",
          "types":[
            "administrative_area_level_2",
            "political"
          ]
        },
        {
          "long_name":"United Kingdom",
          "short_name":"GB",
          "types":[
            "country",
            "political"
          ]
        }
      ],
      "formatted_address":"Trawden, Colne, Lancashire, UK",
      "types":[
        "sublocality",
        "political"
      ]
    }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烈酒灼喉 2024-12-11 09:34:56

是的。如果将其存储在名为 d 的变量中,那么您将使用...

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

等等。请注意其中的 [0],因为带有键 'geometry' 的字典位于列表内。

simplejson.load() 将 JSON 对象映射到 Python dict,将 JSON 列表映射到 list。非常简单;别想太多。


simplejson.dumps() 只是执行与 simplejson.loads() 相反的操作 - 它接受任何标准 Python 对象,并将其转储到一个字符串,该字符串是该对象的 JSON 表示形式目的。例如:

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

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"}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文