无法解析json文件:json.decoder.JSONDecodeError:额外的数据. [英] Can't parse json file: json.decoder.JSONDecodeError: Extra data.

查看:2571
本文介绍了无法解析json文件:json.decoder.JSONDecodeError:额外的数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json文件.它的简化版本如下:

I have a json file. A simplified version of it looks as following:

{
  "host": "a.com",
  "ip": "1.2.2.3",
  "port": 8
}
{
  "host": "b.com",
  "ip": "2.5.0.4",
  "port": 3

}
{
  "host": "c.com",
  "ip": "9.17.6.7",
  "port": 4
}

我运行此脚本parser.py对其进行解析:

I run this script parser.py to parse it:

import json
from pprint import pprint

with open('myfile.json') as f:
    data = json.load(f)
pprint(data)

但是我遇到了这个错误:

But I'm getting this error:

Traceback (most recent call last):
  File "parser.py", line 5, in <module>
    data = json.load(f)
  File "/usr/lib/python3.6/json/__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 6 column 1 (char 54)

能请您指出我所缺少的东西吗?

Can you please point to me what's missing?

推荐答案

您的JSON数据集无效,您可以将它们合并为一个对象数组. 例如:

Your JSON data set is not valid , You can merge them into one array of objects. For example :

[
    {
        "host": "a.com",
        "ip": "1.2.2.3",
        "port": 8
    }, {
        "host": "b.com",
        "ip": "2.5.0.4",
        "port": 3

    }, {
        "host": "c.com",
        "ip": "9.17.6.7",
        "port": 4
    }
]

在JSON中,您不能有多个顶级对象,但是可以有对象数组,并且有效

In JSON you can't have multiple objects of top-level but you can have array of objects and it is valid

如果您想在此链接中看到更多的JSON数据集示例

这篇关于无法解析json文件:json.decoder.JSONDecodeError:额外的数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆