python - Python中的JSON解析错误

标签 python json python-2.7

我正在使用 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,我就可以成功解析数据。任何帮助理解错误并帮助我修复它的帮助将不胜感激。

最佳答案

这不是有效的 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"
   }
]

关于python - Python中的JSON解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22242663/

相关文章:

javascript - 从 JSON 读取数据,对其进行操作并将其写回到 NodeJs 中

python - 如何从 Google Colaboratory 下载文件夹

python - 对 .dat 文件的两列进行一些数学计算并将结果保存到第三列

python - 如何在 Python 3 中使用 https URL 下载图像?

Python PEP 8 文档字符串行长度

javascript - JSON stringify 后的 json_decode 无法正常工作

c# - 用于缩小从 WebMethods 返回的 ASP.NET 3.5 XML 文档的选项

python - 删除包含多个条件的列表条目

python - C++ 中是否有与 python 中的 astype() 函数等效的函数?

python - 如何在python中输入浮点无穷大文字