simplejson.loads() 得到 Invalid \escape: 'x'

发布于 2024-10-04 09:41:33 字数 1438 浏览 5 评论 0原文

我正在学习如何使用 simplejson 来解码 JSON 文件。但我遇到了“无效\转义”错误。 这是代码

import simplejson as json

def main():
    json.loads(r'{"test":"\x27"}')

if __name__ == '__main__':
    main()

这是错误消息

Traceback (most recent call last):
  File "hello_world.py", line 7, in <module>
    main()
  File "hello_world.py", line 4, in main
    json.loads(r'{"test":"\x27"}')
  File "C:\Users\zhangkai\python\simplejson\__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 351, in raw_decode

    obj, end = self.scan_once(s, idx)
  File "C:\Users\zhangkai\python\simplejson\scanner.py", line 36, in _scan_once
    return parse_object((string, idx + 1), encoding, strict, _scan_once, object_
hook)
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 185, in JSONObject

    value, end = scan_once(s, end)
  File "C:\Users\zhangkai\python\simplejson\scanner.py", line 34, in _scan_once
    return parse_string(string, idx + 1, encoding, strict)
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 114, in py_scanstr
ing
    raise ValueError(errmsg(msg, s, end))
ValueError: Invalid \escape: 'x': line 1 column 10 (char 10)

我认为 json 解析器应该识别转义。所以我想知道出了什么问题,我应该做什么。

I am learning how to use simplejson to decode JSON file. But I suffered the "invalid \escape" error.
Here is the code

import simplejson as json

def main():
    json.loads(r'{"test":"\x27"}')

if __name__ == '__main__':
    main()

And here is the error message

Traceback (most recent call last):
  File "hello_world.py", line 7, in <module>
    main()
  File "hello_world.py", line 4, in main
    json.loads(r'{"test":"\x27"}')
  File "C:\Users\zhangkai\python\simplejson\__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 351, in raw_decode

    obj, end = self.scan_once(s, idx)
  File "C:\Users\zhangkai\python\simplejson\scanner.py", line 36, in _scan_once
    return parse_object((string, idx + 1), encoding, strict, _scan_once, object_
hook)
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 185, in JSONObject

    value, end = scan_once(s, end)
  File "C:\Users\zhangkai\python\simplejson\scanner.py", line 34, in _scan_once
    return parse_string(string, idx + 1, encoding, strict)
  File "C:\Users\zhangkai\python\simplejson\decoder.py", line 114, in py_scanstr
ing
    raise ValueError(errmsg(msg, s, end))
ValueError: Invalid \escape: 'x': line 1 column 10 (char 10)

I think json parser is supposed to recognize the escape. So I want to know what is wrong, and what should I do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在夏天 2024-10-11 09:41:33

JSON has no hex escape (\xNN) like some languages (including JavaScript) and notations do, details here. It has a unicode escape, \uNNNN where NNNN is four hex digits, but no \x hex escape.

忆梦 2024-10-11 09:41:33

这是解析器的预期行为,因为 JSON 无效;在字符串中,斜杠后面只能跟随 "\/bfnrtu(后面必须跟 4 个十六进制字符) 不允许使用 x。请参阅 http://json.org/

This is expected behavior from a parser as that JSON is invalid; within a string a slash may be followed only by ", \, /, b, f, n, r, t or u (which must then be followed by 4 hex characters). An x is not allowed. See the spec at http://json.org/

心房的律动 2024-10-11 09:41:33

尝试 python-cjson

import cjson
s = cjson.encode({'abc':123,'def':'xyz'})
print 'json: %s - %s' % (type(s), s)
s = cjson.decode(s)
print '%s - %s' % (type(s), s)

try python-cjson

import cjson
s = cjson.encode({'abc':123,'def':'xyz'})
print 'json: %s - %s' % (type(s), s)
s = cjson.decode(s)
print '%s - %s' % (type(s), s)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文