A brief introduction to the JSON module in Python

Source: Internet
Author: User
(a) 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.

( II) Python JSON module

Python2.6 began to add JSON modules without additional download, and the process of serializing and deserializing the JSON modules of Python is encoding and decoding, respectively. encoding-converts a Python object encoding into a JSON string; decoding-converts the JSON format string decoding to a Python object. To use the JSON module, you must first import:

Import JSON

1, processing of simple data types

The Python JSON module can handle simple data types (string, Unicode, int, float, list, tuple, dict) directly. The Json.dumps () method returns a Str object that has a conversion process from the original Python type to the JSON type in the encoding process, with a specific conversion control as follows:

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

Json.dump (obj, FP, Skipkeys=false, Ensure_ascii=true, Check_circular=true, Allow_nan=true,cls=none, Indent=None, Separators=none, encoding= "Utf-8", Default=none, sort_keys=false,**kw)

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

obj = [[1,2,3],123,123.123, ' abc ', {' key1 ':(), ' key2 ':(4,5,6)}] Encodedjson = Json.dumps (obj) print ' The original list:\n ', obj print ' Length of obj is: ', Len (repr (obj)) print ' repr (obj), replace Whiteblank with *:\n ', repr (obj). replace (' ', ' * ') print ' JSON encoded,replace Whiteblank with *:\n ', Encodedjson.replace (', ' * ')

Output: (The Python Default item separator is ', ' (not ', '), so the list is either converted to a string or a JSON format, and the members are separated by spaces)

The original list: [[1, 2, 3], 123, 123.123, ' abc ', {' Key2 ': (4, 5, 6), ' Key1 ': (1, 2, 3)}] length of obj is:72repr (obj), Replace Whiteblank with *: [[1,*2,*3],*123,*123.123,* ' abc ', *{' Key2 ':* (4,*5,*6), * ' Key1 ':* (1,*2,*3)}] JSON encoded, Replace Whiteblank with *: [[1,*2,*3],*123,*123.123,* "abc", *{"Key2":* [4,*5,*6],* "Key1":* [1,*2,*3]}] 
 
  

We then decode the Encodedjson to get the raw data and the json.loads () function we need to use. The loads method returns the original object, but some conversions of the data type are still occurring, with the ' ABC ' converted to the Unicode type in the example above. It is important to note that the dictionary type key in the JSON string must be double-quoted "" Json.loads () to parse correctly. The type conversions from JSON to Python are compared as follows:

Decodejson = Json.loads (encodedjson) print ' The type of Decodeed obj from JSON: ', type (decodejson) print ' The obj is:\n ', D Ecodejson print ' Length of decoded obj is: ', Len (repr (Decodejson))

Output:

The type of Decodeed obj from JSON: the 
 
  
   
   obj is: [[1, 2, 3], 123, 123.123, u ' abc ', {u ' key2 ': [4, 5, 6], U ' key1 ': [1, 2, 3]}] length of decoded obj is:75 #比原obj多出了3个unicode编码标示 ' u '
 
  

The Sort_keys sorting feature makes the stored data more useful for observation and also makes comparisons of JSON output objects. In the following example, the data1 and data2 data should be the same, and the unordered nature of the dict storage causes the two to be unable to compare.

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 ' sorted data1 (D1): ', D1 print ' unsorted data2 (D2): ', d2 print ' sorted da TA2 (D3): ', D3 print ' D1==d2?: ', d1==d2 print ' d1==d3?: ', D1==d3

Output:

Sorted data1 (D1): {"A": 123, "B": 789, "C": 456} unsorted data2 (D2): {"A": 123, "C": 456, "B": 789} sorted Data2 (D3): {"A" : 123, "B": 789, "C": 456} d1==d2?: False d1==d3?: True

The indent parameter is the indentation meaning, which makes the data storage format more elegant and more readable, which is populated by adding some redundant spaces. However, when decoding (Json.loads ()), the blank padding is removed.

data = {' B ': 789, ' C ': 456, ' a ': 123} D1 = Json.dumps (data,sort_keys=true,indent=4) print ' Data len is: ', Len (repr (data)) print ' 4 indented data:\n ', D1 d2 = json.loads (d1) print ' decoded data: ', repr (D2) print ' Len of decoded data: ', Len (repr (D2 ))

Output: (When loads is visible, the added intent padding space is removed when dumps)

Data Len is:30 4 indented data: {   "a": 123,    "B": 789,    "C": 456} decoded data: {u ' a ': 123, U ' C ': 456, U ' B ': 7 Decoded data:33

JSON is mainly used as a data communication format exists, useless space will waste the communication bandwidth, the appropriate time also to compress the data. The separator parameter can play the role of a tuple that contains a string of split objects, essentially replacing the python default (', ', ': ') delimiter with (', ', ': ').

data = {' B ': 789, ' C ': 456, ' a ': 123} print ' data: ', repr (data) print ' repr (data)       : ', len (data) print ' repr (data) c1/>: ', 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): (data): Dumps (data      , dumps): + indent=2 (data, dumps Ators): 25


Another useful dumps parameter is Skipkeys, which defaults to false. The Dumps method stores Dict objects when key must be of type STR, other types cause typeerror exceptions, and if the Skipkeys is set to true the illegal keys are filtered gracefully.

data = {' B ': 789, ' C ': 456, (): 123} print ' original data: ', repr (data) print ' JSON encoded ', Json.dumps (data,skipkeys= True)

Output:

Original data: {(1, 2): 123, ' C ': 456, ' B ': 789} JSON encoded {"C": 456, "B": 789}

2,json Handling Custom 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.

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

Custom object types and Dict types are converted: encode-defines the function object2dict () stores the object module name, class name, and __dict__ in a dictionary and returns; decode-definition dict2object () resolves the module name, class name, parameter to create a new object and return it. The function that is called during conversion is specified by the default parameter in Json.dumps (), and json.loads () specifies the conversion function by Object_hook.

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 overload the method. For Jsondecoder, so too.

#handling Private Data type #define Class class person (object): Def __init__ (self,name,age): self.name = name SE Lf.age = Age def __repr__ (self): return ' person Object name:%s, age:%d '% (self.name,self.age) #define Transfer functions def object2dict (obj): #convert object to a dict D = {' __class__ ': obj.__class__.__name__, ' __module_  _ ': obj.__module__} d.update (obj.__dict__) return D def dict2object (d): #convert dict to Object if ' __class__ ' in D:class_name = D.pop (' __class__ ') module_name = D.pop (' __module__ ') module = __import__ (module_name) PRI NT ' The module is: ', module class_ = GetAttr (module,class_name)-args = Dict ((Key.encode (' ASCII '), value) for key, Value in D.items ()) #get args print ' The Atrribute: ', repr (args) inst = Class_ (**args) #create new instance Else : Inst = d Return Inst #recreate The default method class Localencoder (JSON. Jsonencoder): def default (self,obj): #convert object to aDict d = {' __class__ ': obj.__class__.__name__, ' __module__ ': obj.__module__} d.update (obj.__dict__) return D Class Localdecoder (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__ (mo Dule_name) Class_ = GetAttr (module,class_name) args = Dict (("Key.encode (' ASCII '), value) for key, value in d.it EMS ()) #get args inst = Class_ (**args) #create new instance else:inst = d Return Inst #test function   if __name__ = = ' __main__ ': p = person (' Aidan ', ') print P #json. Dumps (p) #error'll be throwed d = object2dict (p)      print ' Method-json encode: ', d o = dict2object (d) print ' The decoded obj type:%s, obj:%s '% (Type (o), repr (O)) Dump = Json.dumps (p,default=object2dict) print ' dumps (default = OBJECT2DICT): ', Dump load = json.loads (Dump,object_hook = dict2object) print ' loads (Object_hook = dict2object): ', load d = Loc Alencoder (). Encode (p) o = Localdecoder (). Decode (d) print ' recereated encode method: ', D print ' recereated decode Method: ', type (o), O

Output:

Person Object Name:aidan, age:22 Method-json encode: {' age ': $, ' __module__ ': ' __main__ ', ' __class__ ': ' Person ', ' na '  Me ': ' Aidan '} The module is: 
 
  
   
   the Atrribute: {' age ': ', ' name ': ' Aidan '} The decoded obj type: 
  
   
    
   , Obj:person Object Name:aidan, age:22 dumps (default = object2dict): {"Age": $, "__module__": "__main__", "__class__" : "Person", "name": "Aidan"} The module was: the 
   
    
     
     atrribute: {' age ': $, ' name ': U ' Aidan '} loads (Object_hook = Dict2object): Person Object Name:aidan, age:22 recereated encode method: {"Age": $, "__module__": "__main__", "__cla Ss__ ': ' Person ', ' name ': ' Aidan '} recereated decode method: Person 
    
     
      
      Object Name:aidan, age:22
    
 
     
    
   
  
  • 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.