Home > Backend Development > Python Tutorial > python's json parsing function does not support single quoted strings

python's json parsing function does not support single quoted strings

巴扎黑
Release: 2016-12-03 11:15:44
Original
1719 people have browsed it

Using the json parsing function json.loads of the python standard library cannot use single quotes to quote json strings. The example is as follows:

Use double quotes instead, or call json.dumps(a) before loads.

>>> a = "{'aa':'dd','dd':'df'}"
>>> import json
>>> b = json.loads(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
    obj, end = self._scanner.iterscan(s, **kw).next()
  File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
    rval, next_pos = action(m, context)
  File "/usr/lib64/python2.6/json/decoder.py", line 171, in JSONObject
    raise ValueError(errmsg("Expecting property name", s, end))
ValueError: Expecting property name: line 1 column 1 (char 1)
>>> a = &#39;{"aa":"asdf","dd":"dfads"}
  File "<stdin>", line 1
    a = &#39;{"aa":"asdf","dd":"dfads"}
                                  ^
SyntaxError: EOL while scanning string literal
>>> a = &#39;{"aa":"asdf","dd":"dfads"}&#39;
>>> b = json.loads(a)
>>> b
>>> <span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 1.5; background-color: white;">{u&#39;aa&#39;: u&#39;asdf&#39;, u&#39;dd&#39;: u&#39;dfads&#39;}</span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 1.5; background-color: white;">
</span>
Copy after login


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Backslash present in Json
From 1970-01-01 08:00:00
0
0
0
Get: Transfer JSON data
From 1970-01-01 08:00:00
0
0
0
mysql storage json error
From 1970-01-01 08:00:00
0
0
0
javascript - Problems with displaying json data
From 1970-01-01 08:00:00
0
0
0
Find matching integers in JSON.
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template