Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

python - simplejson.loads() get Invalid escape: 'x'

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:Userszhangkaipythonsimplejson\__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "C:Userszhangkaipythonsimplejsondecoder.py", line 335, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:Userszhangkaipythonsimplejsondecoder.py", line 351, in raw_decode

    obj, end = self.scan_once(s, idx)
  File "C:Userszhangkaipythonsimplejsonscanner.py", line 36, in _scan_once
    return parse_object((string, idx + 1), encoding, strict, _scan_once, object_
hook)
  File "C:Userszhangkaipythonsimplejsondecoder.py", line 185, in JSONObject

    value, end = scan_once(s, end)
  File "C:Userszhangkaipythonsimplejsonscanner.py", line 34, in _scan_once
    return parse_string(string, idx + 1, encoding, strict)
  File "C:Userszhangkaipythonsimplejsondecoder.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...