Detailed explanation of the basic usage of json in python

高洛峰
Release: 2017-02-23 16:48:33
Original
1498 people have browsed it

When using json in Python, the main thing is to use the json module. json is a good format for data interaction, so in many cases, the json data format can be used as the 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
Copy after login

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

{ 
  "中文":"kel", 
  "fist":"kel" 
}
Copy after login


The execution results are as follows:

{u'\u4e2d\u6587': u'kel', u'fist': u'kel'} 
<type &#39;dict&#39;> {u&#39;\u4e2d\u6587&#39;: u&#39;kel&#39;, u&#39;fist&#39;: u&#39;kel&#39;} 
中文 
fist 
{"\u4e2d\u6587": "kel", "fist": "kel"}
Copy after login

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

Note that the parameters in loads must be string, so when opening the file, you must use the read method, otherwise an error will occur.

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 into json format.

I initially encountered an error as follows:

[root@python 56]# python kel.py  
Traceback (most recent call last): 
 File "kel.py", line 5, in <module> 
  json.load(open(&#39;kel.txt&#39;)) 
 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
Copy after login

The main reason is because,,, in the json data format must be The incorrect json file starting with double quotes is as follows:

 { 
  "fist":&#39;kel&#39; 
}
Copy after login

kel.py content is as follows:

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

Double quotes. . . Single quotation marks, I can’t distinguish them stupidly

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

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more detailed explanations of the basic usage of json in python, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template