simplejson.loads()get Invalid \escape:'x' [英] simplejson.loads() get Invalid \escape: 'x'

查看:134
本文介绍了simplejson.loads()get Invalid \escape:'x'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import simplejson as json 

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

如果__name__ =='__main__':
main()

这里是错误消息

 追溯(最近的最后一次调用):
文件hello_world.py,第7行,< module>
main()
文件hello_world.py,第4行,主要
json.loads(r'{test:\x27}')
文件C:\Users\zhangkai\python\simplejson\__init __。py,第307行,加载
return _default_decoder.decode(s)
文件C:\Users \zhangkai\python\simplejson\decoder.py,第335行,解码
obj,end = self.raw_decode(s,idx = _w(s,0).end())
文件C:\Users\zhangkai\python\simplejson\decoder.py,第351行,raw_decode

obj,end = self.scan_once(s,idx)
文件C:\Users\zhangkai\python\simplejson\scanner.py,第36行,在_scan_once
中返回parse_object((string,idx + 1),encoding,strict ,_scan_once,object_
hook)
文件C:\Users\zhangkai\python\simplejson\decoder.py,第185行,JSONObject

value,end = scan_once(s,end)
文件C:\Users\zhangkai\pyt在_scan_once
中返回parse_string(string,idx + 1,encoding,strict)
文件C:\Users\zhangkai\python \simplejson\decoder.py,第114行,在py_scanstr
ing
raise ValueError(errmsg(msg,s,end))
ValueError:Invalid \escape:'x' :第1行第10列(char 10)

我认为json解析器应该识别出逃生。所以我想知道什么是错的,我应该怎么做。

解决方案

JSON没有十六进制转义( \xNN )像一些语言(包括JavaScript)和符号,这里的详细信息。它有一个unicode转义, \uNNNN 其中 NNNN 是四个十六进制数字,但没有 \x hex escape。


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.

解决方案

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.

这篇关于simplejson.loads()get Invalid \escape:'x'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆