A brief introduction to the JSON module in Python _python

Source: Internet
Author: User
Tags in python

(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 to machine parse and generate. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON uses a completely language-independent text format, but it also uses a family of C-language (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 (A collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).

The ordered list of values (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 way. 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 adding JSON modules without additional downloads, and the process of serializing and deserializing Python's JSON modules was encoding and decoding respectively. encoding-converts a Python object encoding into a JSON string; decoding-converts the JSON format string decoding into 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 directly handle simple data types (string, Unicode, int, float, list, tuple, dict). The Json.dumps () method returns a Str object, in which there is a conversion from the original Python type to the JSON type, and the specific conversion is as follows:

Json.dumps method provides a lot of useful parameters to choose from, more commonly used have Sort_keys (Dict object Sorting, we know the default dict is unordered storage), 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 ':(1,2,3], ' key2 ':(4,5,6)}] 
Encodedjson = json.dumps (obj) 
print ' 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 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:72
   repr (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]}] 
<type ' list ' >

We'll then decode the Encodedjson, get the raw data, and use the Json.loads () function. The loads method returns the original object, but some data types are still transformed, and in the example ' ABC ' is converted to Unicode type. It is important to note that the key of the dictionary type in the JSON string must be in double quotes "" Json.loads () to parse correctly. The type conversion from JSON to Python is as follows:

Decodejson = Json.loads (encodedjson) 
print ' type of decodeed obj from JSON: ', type (decodejson) 
print ' obj Is:\n ', Decodejson 
print ' length of decoded obj is: ', Len (repr (Decodejson))

Output:

The type of Decodeed obj from JSON: <type ' list ' > 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 function makes the stored data more useful for observation and makes comparisons of the objects that JSON outputs. In the following example, the data1 and data2 data should be the same, dict the disorder of the storage to make the two impossible comparisons.

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 data2 (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 indented, which makes the data stored in a more elegant and readable format, populated by adding a few redundant spaces. However, when decoding (Json.loads ()), a blank fill is deleted.

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

Output: (Visible loads will be removed when the dumps is added intent padding space)

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

JSON is mainly as a data communication format exists, useless space will waste communication bandwidth, in due course, the data compression. The Separator parameter can act as a tuple that contains a string of split objects, essentially replacing the python default (', ', ': ') delimiter with (', ', ', ') delimiters (', ', ': ').

data = {' B ': 789, ' C ': 456, ' a ': 123} 
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): Dumps (data): Dumps (data 
, indent=2): 46 
dumps (data, separators): 25


Another useful dumps parameter is Skipkeys, which defaults to false. The key must be a str type when the Dumps method stores the Dict object, and other types can cause the TypeError exception to be created and gracefully filter the illegal keys if the Skipkeys is set to true.

data = {' B ': 789, ' C ': 456, (1,2): 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 Processing Custom data types

The JSON module can handle not only the common Python built-in types, but also 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 made because JSON cannot support such an automatic conversion. 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 and then process it. Here, there are two ways to use it.

Method One: Write the transformation function yourself

Custom object type and Dict type conversion: 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, creates a new object and returns. A function called in the conversion process is specified by the default parameter in Json.dumps (), and json.loads () specifies the transformation function by Object_hook.

Method Two: Inherit Jsonencoder and Jsondecoder class, overwrite related method

The Jsonencoder class is responsible for encoding, mainly through its default function, and we can overload the method. For Jsondecoder, too.

#handling Private Data type #define Class 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) #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) print ' 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 a dict 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 I F ' __class__ ' in d:class_name = D.pop (' __class__ ') module_name = D.pop (' __module__ ') module = __impor  t__ (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 the new instance = d return Else:inst #t  est 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 = dic T2object) print ' loads (Object_hook = dict2object): ', load d = Localencoder (). 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 ', ' Name ': ' Aidan '} The 
module is: <module ' __main__ ' from ' D:/project/python/study_json ' > The 
atrribute: {' Age ': +, ' name ': ' Aidan '} The 
decoded obj type: <class ' __main__. Person ', Obj:person Object Name:aidan, age:22 
dumps (default = object2dict): {"Age": "__module__": "__main __ "," __class__ ":" Person "," name ":" Aidan "} The 
module is: <module ' __main__ ' from ' D:/project/python/study_ JSON ' > 
the Atrribute: {' age ': +, ' name ': U ' Aidan '} 
loads (Object_hook = dict2object): Person Object Name:aida N, age:22 
recereated encode method: {' age ': "__module__": "__main__", "__class__": "Person", "name": "Aidan"}
   recereated decode method: <class ' __main__. Person ' > Person Object name:aidan, age:22

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.