Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中解析这个JSON?_Python_Json - Fatal编程技术网

如何在Python中解析这个JSON?

如何在Python中解析这个JSON?,python,json,Python,Json,下面是JSON {"result":[ { "spawn_point_id":"89", "encounter_id":"1421", "expiration_timestamp_ms":"1470105387836", "latitude":38.22, "longitude": -91.27 }, { "distance_in_meters":10, "en

下面是JSON

{"result":[
    {
        "spawn_point_id":"89",
        "encounter_id":"1421",
        "expiration_timestamp_ms":"1470105387836",
        "latitude":38.22,
        "longitude": -91.27
    },
    {
        "distance_in_meters":10,
        "encounter_id":"9677"
    },
    {
        "distance_in_meters":10,
        "encounter_id":"1421"
    },
    {
        "spawn_point_id":"11",
        "encounter_id":"2142",
        "expiration_timestamp_ms":"1470105387444",
        "latitude":38.00,
        "longitude": -91.00
    }
]}
我希望输出像这样

spawn 89 at lat 38.22 long -91.27
spawn 11 at lat 38.00 long -91.00
我使用了json.loads,它实际上使json看起来很时髦

代码如下:

c = json.loads(r.content)
for d in c['result']:
    if d['latitude'] is not None:
        print(str(d['latitude']))
似乎是一种工作,但随后得到错误

Traceback (most recent call last):
File "fast0.py", line 11, in <module>
if d['latitude'] is not None:
KeyError: 'latitude'

您正在寻找一个不存在的密钥。尝试:

c = json.loads(r.content)
for d in c['result']:
    if 'latitude' in d:
        print(str(d['latitude']))

python中有一个json模块。它将加载dict类型的对象,就像Java中的HashMap一样。阅读文档应该会让你受益匪浅。所谓funky,我的意思是它在所有key&valuesTry d的前缀中添加了一个u。获取'latitude'而不是d['latitude']。请不要在你的问题中编辑解决方案。相反,把它作为一个单独的答案贴在下面。请看