Python describes json-related operation instances, and pythonjson

Source: Internet
Author: User

Python describes json-related operation instances, and pythonjson

This article analyzes python's operations on json. We will share this with you for your reference. The details are as follows:

What is json:

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

JSON is constructed in two structures:

A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative 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 to exchange a data format between programming languages that are also based on these structures.

Json official instructions see: http://json.org/

Python to operate json standard api library reference: http://docs.python.org/library/json.html

Encoding and decoding for simple data types:

The simple json. dumps method is used to encode the simple data type, for example:

import jsonobj = [[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]}]

The output result shows that the simple type is similar to the original repr () output result after encode, but some data types are changed, for example, the tuples in the preceding example are converted to lists. During the json encoding process, there will be a conversion process from the original python type to the json type. The specific conversion is as follows:

The json. dumps () method returns an str object encodedjson. We will decode the encodedjson to get the original data. The json. loads () function is required:

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

Output:

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

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

Json. the dumps method provides many useful parameters to choose from. sort_keys (sort dict objects and we know that dict is stored unordered by default), separators, indent, and other parameters are commonly used.

The sorting function makes the stored data easier for observation and compares the json output objects. For example:

data1 = {'b':789,'c':456,'a':123}data2 = {'a':123,'b':789,'c':456}d1 = json.dumps(data1,sort_keys=True)d2 = json.dumps(data2)d3 = json.dumps(data2,sort_keys=True)print d1print d2print d3print d1==d2print d1==d3

Output:

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

In the above example, the data of data1 and data2 should be the same, but they cannot be compared due to the disorder of dict storage. Therefore, the two can be stored by sorting results to avoid inconsistent data. However, after sorting, the system must do more, it will certainly cause certain performance consumption, so proper sorting is very important.

The indent parameter indicates indentation, which makes the data storage format more elegant.

data1 = {'b':789,'c':456,'a':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 more readable, but it is filled by adding redundant spaces. Json is mainly used as a data communication format. network communication is very important to the data size. Useless spaces occupy a lot of communication bandwidth, therefore, data must be compressed as appropriate. The separator parameter can be used to specify a delimiter that contains the string of the object to be separated.

print 'DATA:', repr(data)print 'repr(data)  :', len(repr(data))print 'dumps(data)  :', len(json.dumps(data))print 'dumps(data, indent=2) :', len(json.dumps(data, indent=4))print 'dumps(data, separators):', len(json.dumps(data, separators=(',',':')))

Output:

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

By removing unnecessary blank spaces, the data is compressed, and the effect is obvious.

Another useful dumps parameter is skipkeys. The default value is False. When the dumps method is used to store dict objects, the key must be of the str type. If another type occurs, a TypeError exception occurs. If this parameter is enabled, set it to True, it will be more elegant.

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

Output:

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

Process your own data types

The json module can not only process common python built-in types, but also custom data types. It is often used to process 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 'Person Object name : %s , age : %d' % (self.name,self.age)if __name__ == '__main__': p = Person('Peter',22) print p

If you directly process the Person instance using the json. dumps method, an error is returned because json cannot support automatic conversion. Through the json and python type conversion table mentioned above, we can find that the object type is associated with dict, so we need to convert our custom type to dict, and then proceed. Here, there are two methods to use.

Method 1: Write conversion functions by yourself

'''Created on 2011-12-14@author: Peter'''import Personimport jsonp = Person.Person('Peter',22)def object2dict(obj): #convert object to a dict d = {} d['__class__'] = obj.__class__.__name__ d['__module__'] = obj.__module__ d.update(obj.__dict__) return ddef dict2object(d): #convert dict to object if'__class__' in d: class_name = d.pop('__class__') module_name = d.pop('__module__') module = __import__(module_name) class_ = getattr(module,class_name) args = dict((key.encode('ascii'), value) for key, value in d.items()) #get args inst = class_(**args) #create new instance else: inst = d return instd = object2dict(p)print d#{'age': 22, '__module__': 'Person', '__class__': 'Person', 'name': 'Peter'}o = dict2object(d)print type(o),o#<class 'Person.Person'> Person Object name : Peter , age : 22dump = 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

The code above has been clearly written. The essence is to convert the custom object type and dict type. The object2dict function stores the Object Module name, class name, and _ dict _ in the dict object and returns the result. The dict2object function is used to decompress the module name, class name, and parameter, create a new object, and return the result. Add the default parameter to the json. dumps method. This parameter indicates that the specified function is called during the conversion process, and the object_hook is added to the json. loads method during the decode process to specify the conversion function.

Method 2: Inherit the JSONEncoder and JSONDecoder classes and override related methods

The JSONEncoder class is responsible for encoding, mainly through its default Function Conversion. We can override this method. Similarly, for JSONDecoder.

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

The JSONDecoder class methods are slightly different, but it is not difficult to rewrite them. The Code should be clear.

 PS: For json operations, we recommend several useful json online tools for your reference:

Online JSON code verification, validation, beautification, and formatting tools:
Http://tools.jb51.net/code/json

JSONOnline formatting tool:
Http://tools.jb51.net/code/jsonformat

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

JsonCode Online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

Online json compression/escape tools:
Http://tools.jb51.net/code/json_yasuo_trans

C language style/HTML/CSS/json code formatting and beautification tools:
Http://tools.jb51.net/code/ccode_html_css_json

For more Python-related content, refer to this topic: python json operation skills summary, Python encoding operation skills summary, Python image operation skills summary, Python data structure and algorithm tutorial, Python Socket programming skills Summary python function usage tips summary, Python string operation tips summary, Python getting started and advanced classic tutorials, and Python file and directory operations tips Summary

I hope this article will help you with Python programming.

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.