Home  >  Article  >  Backend Development  >  Detailed explanation of python related operation examples on json

Detailed explanation of python related operation examples on json

高洛峰
高洛峰Original
2017-01-07 13:17:401190browse

The example of this article analyzes the related operations of python on json. Share it with everyone for your reference, the details are as follows:

What is json:

JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language.

JSON is constructed from two structures:

A collection of name/value pairs. In different languages, it is understood as an object, a record, a struct, a dictionary, a hash table, a keyed list, or an associative array. array).

An ordered list of values. In most languages, it is understood as an array.

These are common data structures. In fact most modern computer languages ​​support them in some form. This makes it possible for a data format to be exchanged between programming languages ​​that are also based on these structures.

For official instructions on json, see: http://json.org/

Standard api library reference for Python operations on json: http://docs.python.org/library/json.html

Encoding and decoding of simple data types:

Use the simple json.dumps method to encode simple data types, for example:

import json
obj = [[1,2,3],123,123.123,'abc',{'key1':(1,2,3),'key2':(4,5,6)}]
encodedjson = json.dumps(obj)
print repr(obj)
print encodedjson

Output:

[[1, 2, 3], 123, 123.123, 'abc', {'key2': (4, 5, 6), 'key1': (1, 2, 3)}]
[[1, 2, 3], 123, 123.123, "abc", {"key2": [4, 5, 6], "key1": [1, 2, 3]}]

It can be seen from the output results that the simple type after encoding is very similar to its original repr() output, but some data types have been changed, such as the tuple in the above example being converted to a list. During the encoding process of json, there will be a conversion process from python original type to json type. The specific conversion comparison is as follows:

Detailed explanation of python related operation examples on json

json.dumps() method returns a str object encodedjson, we will decode encodedjson next to get the original data. The json.loads() function that needs to be used:

decodejson = json.loads(encodedjson)
print type(decodejson)
print decodejson[4]['key1']
print decodejson

Output:

<type &#39;list&#39;>
[1, 2, 3]
[[1, 2, 3], 123, 123.123, u&#39;abc&#39;, {u&#39;key2&#39;: [4, 5, 6], u&#39;key1&#39;: [1, 2, 3]}]

The loads method returns the original object, but some data type conversions still occur. For example, in the above example, 'abc' is converted to unicode type. The comparison of type conversion from json to python is as follows:

Detailed explanation of python related operation examples on json

The json.dumps method provides many useful parameters to choose from. The more commonly used ones are sort_keys (for dict objects) Sorting, we know that the default dict is stored unordered), separators, indent and other parameters.

The sorting function makes the stored data more conducive to observation, and also enables comparison of json output objects, for example:

data1 = {&#39;b&#39;:789,&#39;c&#39;:456,&#39;a&#39;:123}
data2 = {&#39;a&#39;:123,&#39;b&#39;:789,&#39;c&#39;:456}
d1 = json.dumps(data1,sort_keys=True)
d2 = json.dumps(data2)
d3 = json.dumps(data2,sort_keys=True)
print d1
print d2
print d3
print d1==d2
print d1==d3

Output:

{"a": 123, "b": 789, "c": 456}
{"a": 123, "c": 456, "b": 789}
{"a": 123, "b": 789, "c": 456}
False
True

In the above example, originally data1 and data2 data should be the same, but due to the unordered nature of dict storage, the two cannot be compared. Therefore, the two can be stored through the sorted results to avoid data inconsistency. However, before storing after sorting, the system must do more things, which will definitely cause a certain amount of performance consumption, so appropriate sorting is very important.

The indent parameter means indentation, which can make the format of data storage more elegant.

data1 = {&#39;b&#39;:789,&#39;c&#39;:456,&#39;a&#39;:123}
d1 = json.dumps(data1,sort_keys=True,indent=4)
print d1

Output:

{
 "a": 123,
 "b": 789,
 "c": 456
}

After the output data is formatted, it becomes It's more readable, but it's filled in by adding some redundant white spaces. JSON mainly exists as a data communication format, and network communication is very concerned about the size of the data. Useless spaces will occupy a lot of communication bandwidth, so the data must be compressed when appropriate. The separator parameter can play this role. The parameter passed is a tuple containing the string of the split object.

print &#39;DATA:&#39;, repr(data)
print &#39;repr(data)  :&#39;, len(repr(data))
print &#39;dumps(data)  :&#39;, len(json.dumps(data))
print &#39;dumps(data, indent=2) :&#39;, len(json.dumps(data, indent=4))
print &#39;dumps(data, separators):&#39;, len(json.dumps(data, separators=(&#39;,&#39;,&#39;:&#39;)))

Output:

DATA: {&#39;a&#39;: 123, &#39;c&#39;: 456, &#39;b&#39;: 789}
repr(data)  : 30
dumps(data)  : 30
dumps(data, indent=2) : 46
dumps(data, separators): 25

By removing extra whitespace characters, this is achieved The purpose of compressing data is quite obvious.

Another useful dumps parameter is skipkeys, which defaults to False. When the dumps method stores a dict object, the key must be of str type. If other types occur, a TypeError exception will be generated. If this parameter is turned on and set to True, it will be more graceful.

data = {&#39;b&#39;:789,&#39;c&#39;:456,(1,2):123}
print json.dumps(data,skipkeys=True)

Output:

{"c": 456, "b": 789}

Process your own data type

## The #json module can not only handle ordinary Python built-in types, but also our custom data types, and it is often common to handle custom objects.

First, we define a class Person.

class Person(object):
 def __init__(self,name,age):
 self.name = name
 self.age = age
 def __repr__(self):
 return &#39;Person Object name : %s , age : %d&#39; % (self.name,self.age)
if __name__ == &#39;__main__&#39;:
 p = Person(&#39;Peter&#39;,22)
 print p

如果直接通过json.dumps方法对Person的实例进行处理的话,会报错,因为json无法支持这样的自动转化。通过上面所提到的json和python的类型转化对照表,可以发现,object类型是和dict相关联的,所以我们需要把我们自定义的类型转化为dict,然后再进行处理。这里,有两种方法可以使用。

方法一:自己写转化函数

&#39;&#39;&#39;
Created on 2011-12-14
@author: Peter
&#39;&#39;&#39;
import Person
import json
p = Person.Person(&#39;Peter&#39;,22)
def object2dict(obj):
 #convert object to a dict
 d = {}
 d[&#39;__class__&#39;] = obj.__class__.__name__
 d[&#39;__module__&#39;] = obj.__module__
 d.update(obj.__dict__)
 return d
def dict2object(d):
 #convert dict to object
 if&#39;__class__&#39; in d:
 class_name = d.pop(&#39;__class__&#39;)
 module_name = d.pop(&#39;__module__&#39;)
 module = __import__(module_name)
 class_ = getattr(module,class_name)
 args = dict((key.encode(&#39;ascii&#39;), value) for key, value in d.items()) #get args
 inst = class_(**args) #create new instance
 else:
 inst = d
 return inst
d = object2dict(p)
print d
#{&#39;age&#39;: 22, &#39;__module__&#39;: &#39;Person&#39;, &#39;__class__&#39;: &#39;Person&#39;, &#39;name&#39;: &#39;Peter&#39;}
o = dict2object(d)
print type(o),o
#<class &#39;Person.Person&#39;> Person Object name : Peter , age : 22
dump = json.dumps(p,default=object2dict)
print dump
#{"age": 22, "__module__": "Person", "__class__": "Person", "name": "Peter"}
load = json.loads(dump,object_hook = dict2object)
print load
#Person Object name : Peter , age : 22

   

上面代码已经写的很清楚了,实质就是自定义object类型和dict类型进行转化。object2dict函数将对象模块名、类名以及__dict__存储在dict对象里,并返回。dict2object函数则是反解出模块名、类名、参数,创建新的对象并返回。在json.dumps 方法中增加default参数,该参数表示在转化过程中调用指定的函数,同样在decode过程中json.loads方法增加object_hook,指定转化函数。

方法二:继承JSONEncoder和JSONDecoder类,覆写相关方法

JSONEncoder类负责编码,主要是通过其default函数进行转化,我们可以override该方法。同理对于JSONDecoder。

&#39;&#39;&#39;
Created on 2011-12-14
@author: Peter
&#39;&#39;&#39;
import Person
import json
p = Person.Person(&#39;Peter&#39;,22)
class MyEncoder(json.JSONEncoder):
 def default(self,obj):
 #convert object to a dict
 d = {}
 d[&#39;__class__&#39;] = obj.__class__.__name__
 d[&#39;__module__&#39;] = obj.__module__
 d.update(obj.__dict__)
 return d
class MyDecoder(json.JSONDecoder):
 def __init__(self):
 json.JSONDecoder.__init__(self,object_hook=self.dict2object)
 def dict2object(self,d):
 #convert dict to object
 if&#39;__class__&#39; in d:
  class_name = d.pop(&#39;__class__&#39;)
  module_name = d.pop(&#39;__module__&#39;)
  module = __import__(module_name)
  class_ = getattr(module,class_name)
  args = dict((key.encode(&#39;ascii&#39;), value) for key, value in d.items()) #get args
  inst = class_(**args) #create new instance
 else:
  inst = d
 return inst
d = MyEncoder().encode(p)
o = MyDecoder().decode(d)
print d
print type(o), o

   

对于JSONDecoder类方法,稍微有点不同,但是改写起来也不是很麻烦。看代码应该就比较清楚了。

希望本文所述对大家Python程序设计有所帮助。

更多Detailed explanation of python related operation examples on json相关文章请关注PHP中文网!

Statement:
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