JSON overview and Python-related operations on JSON "turn"

Source: Internet
Author: User

What is JSON:

JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:

A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array).
The sequence of values (an ordered list of values). In most languages, it is understood as an array (array).
These are common data structures. In fact, most of the 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 constructs.

JSO Official notes See: http://json.org/

Standard API Library Reference for Python operation JSON: http://docs.python.org/library/json.html

Encoding and decoding for simple data types:

Use a simple Json.dumps method to encode a simple data type, for example:

 1  import   JSON  2  3  obj = [[1,2,3],123,123.123, '   key1   ' :(-in-the-box ',  

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 result of the output shows that the simple type is very similar to its original repr () output after encode, but some data types have changed, for example, the tuple in the previous example is converted to a list. During the encoding process of JSON, there is a conversion process from the original Python type to the JSON type, with the specific conversions as follows:

The Json.dumps () method returns a Str object Encodedjson, and we then decode the Encodedjson to get the raw data and the json.loads () function that we need to use:

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 type conversions are still occurring. For example, in the example above, ' ABC ' was converted to a Unicode type. The type conversions from JSON to Python are compared as follows:

Json.dumps method provides a lot of useful parameters to choose from, more commonly used have Sort_keys (Dict objects to sort, we know the default dict is unordered), separators,indent and other parameters.

The sorting feature makes the stored data more useful for observation and also makes comparisons of JSON output objects, such as:

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)PrintD1PrintD2PrintD3Printd1==D2PrintD1==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, the data1 and data2 data should be the same, but because of the unordered nature of dict storage, they cannot be compared. Therefore, the two can be sorted by the results of storage to avoid the inconsistent data, but after sorting and then storage, the system must do something more, it will certainly result in a certain amount of performance consumption, so proper sequencing is very important.

The indent parameter is the indentation meaning, which can make the data storage format more elegant.

Data1 = {'b': 789,'C': 456,'a' : 123= Json.dumps (data1,sort_keys=true,indent=4)print d1

Output:

{
"A": 123,
"B": 789,
"C": 456
}

After the output is formatted, it becomes more readable, but it is populated by adding some redundant blanks. JSON is mainly as a data communication format exists, and network communication is very concerned about the size of the information, useless space will occupy a lot of communication bandwidth, so the appropriate time also to compress the data. The separator parameter can play the role of a tuple that contains a string that splits the object.

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): 30
Dumps (data): 30
Dumps (data, indent=2): 46
Dumps (data, separators): 25

The purpose of compressing the data is achieved by removing the extra whitespace characters, and the effect is quite obvious.

Another useful dumps parameter is Skipkeys, which defaults to false. When the dumps method stores the Dict object, the key must be of type STR, and if there are other types, a TypeError exception will be generated, and if this argument is set to true, it will be more gracefully over.

data = {'b': 789,'C': 456, (UP): 123}Print Json.dumps (Data,skipkeys=true)

Output:

{"C": 456, "B": 789}

Working on your own data types

The JSON module not only handles common python built-in types, but also handles our custom data types, which are often used to handle custom objects.

First, we define a class person.

classPerson (object):def __init__(self,name,age): Self.name=name Self.age= Agedef __repr__(self):return 'Person Object Name:%s, age:%d'%(self.name,self.age)if __name__=='__main__': P= Person ('Peter', 22)    PrintP

If the instance of person is processed directly through the Json.dumps method, an error is given, because JSON cannot support such an automatic conversion. By using the JSON and Python type conversion tables mentioned above, we can see that the object type is associated with dict, so we need to convert our custom type to dict before processing. Here, there are two ways to use it.

Method One: Write your own conversion function

" "Created on 2011-12-14@author:peter" "Import PersonImportJSON P= Person.person ('Peter', 22) defobject2dict (obj):#Convert object to a dictD ={} d['__class__'] = obj.__class__.__name__d['__module__'] = obj.__module__d.update (obj.__dict__)    returnDdefDict2object (d):#convert Dict to Object    if'__class__' inchD: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) forKey, ValueinchD.items ())#Get argsInst = Class_ (**args)#Create new instance    Else: Inst=DreturnInst D=object2dict (P)PrintD#{' age ': $, ' __module__ ': ' Person ', ' __class__ ': ' Person ', ' name ': ' Peter '}o=Dict2object (d)Printtype (o), O#<class ' Person.person ' > Person Object name:peter, age:22Dump= Json.dumps (p,default=object2dict)PrintDump#{"Age": $, "__module__": "Person", "__class__": "Person", "name": "Peter"}Load= Json.loads (Dump,object_hook =dict2object)PrintLoad#Person Object Name:peter, age:22

The above code has been written very clearly, the essence is the custom object type and Dict type to convert. The Object2dict function stores the object module name, class name, and __dict__ in the Dict object and returns. The Dict2object function is to reverse the module name, the class name, the parameters, create a new object, and return. Add the default parameter to the Json.dumps method, which means that the specified function is called during the conversion process, and the Json.loads method adds object_hook to the decode process, specifying the conversion function.

Method Two: Inherit Jsonencoder and Jsondecoder classes, overwrite related methods

The Jsonencoder class is responsible for encoding, mainly through its default function to convert, we can override the method. Likewise for Jsondecoder.

" "Created on 2011-12-14@author:peter" "Import PersonImportJSON P= Person.person ('Peter', 22) classMyencoder (JSON. Jsonencoder):defDefault (self,obj):#Convert object to a dictD ={} d['__class__'] = obj.__class__.__name__d['__module__'] = obj.__module__d.update (obj.__dict__)        returnDclassMydecoder (JSON. Jsondecoder):def __init__(self): JSON. Jsondecoder.__init__(self,object_hook=self.dict2object)defDict2object (self,d):#convert Dict to Object        if'__class__' inchD: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) forKey, ValueinchD.items ())#Get argsInst = Class_ (**args)#Create new instance        Else: Inst=DreturnInst D=Myencoder (). Encode (P) O=Mydecoder (). Decode (d)PrintDPrintType (o), O

For the Jsondecoder class method, it's a little bit different, but it's not too cumbersome to rewrite. Look at the code should be more clear.

Source: http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html

JSON overview and Python-related operations on JSON "turn"

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.