Basic usage of json in python

Source: Internet
Author: User
This article mainly introduces the basic usage of json in python. when using json in Python, it mainly uses the json module, json is a good format for data interaction. if you are interested, take a look. When using json in Python, the json module is mainly used. json is used for data interaction in a good format. in many cases, json data format can be used as an interface between programs.

#!/usr/bin/env python #-*- coding:utf-8 -*- import json  print json.load(open('kel.txt')) #deserialize string or unicode to python object j = json.loads(open('kel.txt').read(),encoding='utf-8') print type(j),j for i in j:   print i k = json.dumps(j,encoding='utf-8').decode('utf-8') print k

The content of the kel.txt file is as follows:

{"Chinese": "kel", "fist": "kel "}


The execution result is as follows:

{U' \ u4e2d \ u6587 ': u'kel', u'fist ': u'kel '}
 
  
{U' \ u4e2d \ u6587 ': u'kel', u'fist ': u'kel'} Chinese fist {"\ u4e2d \ u6587": "kel ", "fist": "kel "}
 

The main methods used here are json. loads and json. dumps.

Note that the parameter in loads must be string, so you must use the read method when opening the file. Otherwise, an error occurs.

The loads method is mainly used to load json data into objects in python, while the dumps method is mainly used to modify python objects to json format.

An error occurs as follows:

[root@python 56]# python kel.py  Traceback (most recent call last):  File "kel.py", line 5, in 
 
     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, it must start with double quotation marks. the incorrect json file is as follows:

 {   "fist":'kel' }

The contents of kel. py are as follows:

 #!/usr/bin/env python #-*- coding:utf-8 -*- import json j = json.loads(open('kel.txt').read()) print type(j),j

Double quotation marks... Single quotes, silly points unclear

Sometimes, when performing the loads method, the single quotation mark string is generated... This is especially true in python. it has nothing to do with other things, mainly because of the quotation marks !!!

The above is all the content of this article. I hope it will help you learn and support PHP.

For more details about the basic usage of json in python, refer to the PHP Chinese network!

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.