Python json dumps and loads may have made a mistake

Source: Internet
Author: User

Tag:ini    Note    obj   file   ext    bsp    convert     string    span   

Json.dumps () is the conversion of a Python data structure into a JSON-encoded string json.loads () to convert a JSON-encoded string into a Python data structure    the following:>>> Import json>>> a={' name ': ' Xiaoming '}>>> json.dumps (a) ' {' name ': ' Xiaoming '} ' >>> b= ' {' Name ":" Xiaoming "} ' >>> Json.loads (b) {u ' name ': U ' xiaoming '}   Note: We originally defined the elements of a as single quotes, and dumps transformed into double quotes, When the loads becomes a Python variable, the elements become single quotes, and the string is added with a U.   Then when I define the following string type, the element contents are in single quotes, and the entire string is enclosed in double quotes:>>> c= "{' name ': ' Xiaoming '}" >>> print Json.loads (c) Traceback (most recent): File "<stdin>", line 1, in <module>file "/usr/local/python27/lib/python2.7 /json/__init__.py ", line 339, in Loadsreturn _default_decoder.decode (s) File"/usr/local/python27/lib/python2.7/json/ decoder.py ", line 364, in decodeobj, end = Self.raw_decode (S, idx=_w (s, 0). End ()) File"/usr/local/python27/lib/python2.7 /json/decoder.py ", line 380, in raw_decodeobj, end = Self.scan_once (s, idx) valueerror:expecting property Name:line 1 col Umn 2 (char 1)   program error, so General Requirements when a string is converted to a Python data type by loads, the outer layer is enclosed in single quotes, and the element key and value in double quotation marksAlso, even if the above conditions are met, when the Unicode with the U prefix is modified, the error:>>> c= ' {u ' name ": U" xiaoming "} ' >>> json.loads (c) Traceback ( Most recent call last): File "<stdin>", line 1, in <module>file "/usr/local/python27/lib/python2.7/json/__ init__.py ", line 339, in Loadsreturn _default_decoder.decode (s) File"/usr/local/python27/lib/python2.7/json/ decoder.py ", line 364, in decodeobj, end = Self.raw_decode (S, idx=_w (s, 0). End ()) File"/usr/local/python27/lib/python2.7 /json/decoder.py ", line 380, in raw_decodeobj, end = Self.scan_once (s, idx) valueerror:expecting property Name:line 1 col Umn 2 (char 1)so loads in the process, you also need to remove the u prefix modifier--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------

Python json dumps and loads may have made a mistake

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.