Basic ways to use JSON in Python

Source: Internet
Author: User

When using JSON in Python, it is mainly the use of JSON modules, JSON is in a good format for data interaction, so in many cases, you can use the JSON data format as the interface between programs,

#!/usr/bin/env python#-*-coding:utf-8-*-import jsonprint json.load (open (' Kel.txt ')) #deserialize string or Unicode to Python OBJECTJ = json.loads (open (' kel.txt '). Read (), encoding= ' utf-8 ') print type (j), Jfor I in J:    Print ik = json.dumps (j,encoding= ' Utf-8 '). Decode (' Utf-8 ') print K

The contents of the Kel.txt file are as follows:

{    "Chinese": "Kel",    "fist": "Kel"}

The results of the implementation are as follows:

{u ' \u4e2d\u6587 ': U ' kel ', U ' fist ': U ' kel '}<type ' dict ' > {u ' \u4e2d\u6587 ': U ' kel ', U ' fist ': U ' kel '} Chinese fist{"\u4e2d \u6587 ":" Kel "," fist ":" Kel "}

The main methods used in these are json.loads and json.dumps

Note that the argument must be string in loads, so that when the file is opened, the Read method is used, otherwise an error occurs.

The loads method is primarily used to load JSON data into objects in Python, and the Dumps method is primarily to modify the Python object to JSON format.


Start encountering an error as follows:

[email protected] 56]# python kel.py Traceback (most recent call last):  File ' kel.py ', line 5, <module>    Json.load (Open (' Kel.txt '))  File "/usr/local/python/lib/python2.7/json/__init__.py", line 291, in load    **kw)  File "/usr/local/python/lib/python2.7/json/__init__.py", line 339, in loads    return _default_ Decoder.decode (s)  File "/usr/local/python/lib/python2.7/json/decoder.py", line 364, in decode    obj, end = Self.raw_decode (S, idx=_w (s, 0). End ())  File "/usr/local/python/lib/python2.7/json/decoder.py", line 382, in Raw_ Decode    Raise ValueError ("No JSON object could be decoded") Valueerror:no JSON object could be decoded
The main reason is that, in the JSON data format, you must start with double quotes, and the wrong JSON file is as follows:

{    "fist": ' Kel '}
kel.py content is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-import jsonj = json.loads (open (' kel.txt '). Read ()) print type (j), j~                  
Double quotes ... Single quotes, silly points are not clear


Sometimes, when the loads method is performed, it is because of the string that produces the single quote ... This is especially true in Python, which has nothing to do with other things, mainly the relationship between quotes!!!




Basic ways to use JSON in Python

Related Article

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.