【问题标题】:JSON parse error in PythonPython中的JSON解析错误
【发布时间】:2014-04-10 03:53:09
【问题描述】:

我正在使用 Python 2.7.6 来解析 JSON 文件,但我遇到了一个错误,我不确定为什么。这是我第一次与 Python 打交道,所以这可能是一个非常基本的问题,但我看过一些关于堆栈溢出的内容,似乎无法找出问题所在。

这是我解析数据的python代码:

import json
from pprint import pprint 

with open ('test.json') as data_file:
    data = json.load(data_file);
pprint(data)  

这是我的 JSON 文件:

{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "hckr9Hf8BUHcXfOSDv9eJA", "review_id": "K6EEJo0I8AbwGWvwe5SJYQ", "stars": 5, "date": "2013-05-05", "text": "This place is fantastic. they have a restaurant inside the grocery store. very good food.", "type": "review", "business_id": "uPezkdNi_x_SwWlf_2rcMw"}
{"votes": {"funny": 0, "useful": 0, "cool": 1}, "user_id": "PK3TxomYLwZuOXonmYqjNw", "review_id": "5ivy-tczAQ4WYrmVF6YoKg", "stars": 5, "date": "2013-08-11", "text": "This is going to be a place we go back to many times!", "type": "review", "business_id": "UB2j_EV3CIM_E4LcpadKMQ"}

这是我在解析 JSON 时遇到的错误:

File "./parse.py", line 6, in <module>
    data = json.load(data_file);
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 368, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 3 column 1 (char 318 - 600)

奇怪的是,如果我只有第一行 JSON,我就可以成功解析数据。任何帮助理解错误并帮助我修复它都将不胜感激。

【问题讨论】:

  • 你想要for line in data_file - 或者,在你的行尾加上括号[]和逗号。
  • 在顶层,有效的 JSON 文件必须single 对象 {...}single数组[...];你不能有两个背靠背的对象。

标签: python json python-2.7


【解决方案1】:

这不是有效的 json - 你不能像这样将两个散列放在一起......尝试这样的事情。你会注意到我在中间放了一个逗号,并将整个散列集放在一个数组中。

[
   {
      "stars" : 5,
      "date" : "2013-05-05",
      "review_id" : "K6EEJo0I8AbwGWvwe5SJYQ",
      "text" : "This place is fantastic. they have a restaurant inside the grocery store. very good food.",
      "user_id" : "hckr9Hf8BUHcXfOSDv9eJA",
      "type" : "review",
      "votes" : {
         "funny" : 0,
         "cool" : 0,
         "useful" : 0
      },
      "business_id" : "uPezkdNi_x_SwWlf_2rcMw"
   },
   {
      "stars" : 5,
      "date" : "2013-08-11",
      "review_id" : "5ivy-tczAQ4WYrmVF6YoKg",
      "text" : "This is going to be a place we go back to many times!",
      "user_id" : "PK3TxomYLwZuOXonmYqjNw",
      "type" : "review",
      "votes" : {
         "funny" : 0,
         "cool" : 1,
         "useful" : 0
      },
      "business_id" : "UB2j_EV3CIM_E4LcpadKMQ"
   }
]

【讨论】:

  • 很有趣,因为我是从 Yelp 数据集中得到的。所以他们给了我无效的 JSON?
  • 这 2 个 JSON 哈希是独立有效的,所以也许您只是以他们没有预料到的方式将它们连接在一起?或者它们可能是独立有效的,而不是一起有效的。如果您愿意,您可以一次解析它们(如@g.d.d.c 建议的那样逐行解析)!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多