json.loads在JSON有效时抛出错误。

huangapple go评论80阅读模式
英文:

json.loads throwing error when the JSON is valid

问题

我有可以从这里下载的JSON数据(太长无法在这里粘贴)。此数据从文件中读取并使用 json.loads(str) 加载。例如:

path = open(file_path_presented).read())
try:
    retval = json.load(open(path))
except Exception as e:
    if "File name too long" in str(e):
        retval = json.loads(path)
        # 在这里出现了链接中的JSON错误

每次加载此JSON时,我都会收到以下错误:

EXCEPTION INFORMATION: Traceback (most recent call):
  File "/mnt/c/Users/rando/bin/python/mt/api/api_handler.py", line 932, in get
    retval = json.loads(str(path))
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
Unterminated string starting at: line 1 column 25 (char 24)

我最好的猜测是,这个错误与JSON数据中的 {} 字符没有正确转义有关,因为它仅在某些情况下发生,而不是每次都发生。我已经使用多个代码检查工具和 jq,它们都表示JSON是有效的JSON。如何在不引发错误的情况下加载此JSON数据?

我尝试将数据加载为 StringIOjson.load(io) 中:

from StringIO import StringIO

io = StringIO(path)
retval = json.load(io)

以及使用正则表达式修复JSON,以及使用 json.loads(path, strict=False),但这些方法都产生了相同的错误。

英文:

I have json data that you can download from here (its too long to paste here). This data is being read from a file and loaded using json.loads(str). For example:

path = open(file_path_presented).read())
try:
    retval = json.load(open(path))
except Exception as e:
    if "File name too long" in str(e):
        retval = json.loads(path)
        # erroring out here with the json in the link

Every time I load this json I receive the following error:

EXCEPTION INFORMATION: Traceback (most recent call):
  File "/mnt/c/Users/rando/bin/python/mt/api/api_handler.py", line 932, in get
    retval = json.loads(str(path))
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
    obj, end = self.scan_once(s, idx)
Unterminated string starting at: line 1 column 25 (char 24)

My best guess is that this error has something to do with the json data having { and } characters in it that are not being escaped properly because it only happens sometimes and not all the time. I've used multiple linters and all of them say that the JSON is valid JSON, along with jq. How can I load this json with throwing an error?

I've tried loading the data as a StringIO into json.load(io) with:

from StringIO import StringIO

io = StringIO(path)
retval = json.load(io)

As well as fixing the JSON using a regex, and using json.loads(path, strict=False) However these all produce the sane error

答案1

得分: 1

以下是翻译好的内容:

"JSON 数据似乎没有问题。

您可能没有使用最适合将其转换为 Python 字典的技术。

考虑以下代码:

import requests
import json

TARGET = '/Volumes/G-Drive/foo.json'
with requests.get('https://pastebin.com/raw/X8f4Nz6v', stream=True) as r:
    r.raise_for_status()
    with open(TARGET, 'wb') as j:
        for chunk in r.iter_content(4096):
            j.write(chunk)
with open(TARGET) as j:
    d = json.load(j)

此代码可以正常运行,不会出现任何异常。

可选:

如果您不想创建本地文件,您可以按如下方式构建一个 bytearray:

with requests.get('https://pastebin.com/raw/X8f4Nz6v', stream=True) as r:
    r.raise_for_status()
    b = bytearray()
    for chunk in r.iter_content(4096):
        b += chunk
    d = json.loads(b.decode())

希望这对您有所帮助。

英文:

There doesn't appear to be anything wrong with the JSON data.

You may not be using the most appropriate technique for converting it to a Python dictionary.

Consider this:

import requests
import json

TARGET = '/Volumes/G-Drive/foo.json'
with requests.get('https://pastebin.com/raw/X8f4Nz6v', stream=True) as r:
    r.raise_for_status()
    with open(TARGET, 'wb') as j:
        for chunk in r.iter_content(4096):
            j.write(chunk)
with open(TARGET) as j:
    d = json.load(j)

This code runs to completion without any exception(s)

Optionally:

If you don't want to create a local file you can just build a bytearray as follows:

with requests.get('https://pastebin.com/raw/X8f4Nz6v', stream=True) as r:
    r.raise_for_status()
    b = bytearray()
    for chunk in r.iter_content(4096):
        b += chunk
    d = json.loads(b.decode())

huangapple
  • 本文由 发表于 2023年6月15日 02:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定