I'm using Python 3.5.2 on Windows 10 x64. The JSON file I'm reading is this which is a JSON array containing 2 more arrays.

我正在try 使用json模块解析这个JSON文件.如docs中所述,JSON文件必须符合RFC 7159.我判断了我的文件here,它告诉我它与RFC 7159格式完全兼容,但是当try 使用以下简单的python代码读取它时:

with open(absolute_json_file_path, encoding='utf-8-sig') as json_file:
    text = json_file.read()
    json_data = json.load(json_file)
    print(json_data)

我得到了一个例外:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 2217, in <module>
    globals = debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\pydevd.py", line 1643, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
  File "C:/Users/Andres Torti/Git-Repos/MCF/Sur3D.App/shapes-json-checker.py", line 14, in <module>
    json_data = json.load(json_file)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Andres Torti\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I can read this exact file perfectly fine on Javascript but I can't get Python to parse it. Is there anything wrong with my file or is any problem with the Python parser?

推荐答案

Based on reading over the documentation again, it appears you need to either change the third line to

json_data = json.loads(text)

或删除该行

text = json_file.read()

since read() causes the file's index to reach the end of the file. (I suppose, alternatively, you can reset the index of the file).

Json相关问答推荐

SWIFT中的网络经理

Python将Pandas转换为嵌套的JSON

褐煤面积图中的分选问题

在Jolt中将具有嵌套元素的对象数组转换为单独的对象列表

对一些JSON模式验证的混淆

简单条形图和有序分类中条形图的 colored颜色 梯度

规范化JSON数据

爆炸没有数组的 struct pyspark

将 GEOSwift.JSON 转换为 Swift 中的 struct

遍历 JSON,检索父值

将 JSON 字符串解析为 Kotlin Android 中的对象列表(MOSHI?)

NiFi 使用嵌套数组将 avro 转换为 JSON 数组格式

如何将该 JSON 解析为 Kotlin 类?

如何使用 ConfigurationBuilder 解析现有的 json 字符串(不是文件)

hook到 Decodable.init() 以获得未指定的键?

Json.NET SerializeObject 转义值以防止 XSS

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

十六进制格式可以与 JSON 文件一起使用吗?如果是这样,怎么做?

Spring MVC控制器中的JSON参数

如何在已声明的 JSON 对象中添加键值对