Python's json module Application Summary

JSON (the Java Script Object Notation) : a lightweight data interchange format, XML relative terms more simple, easy to read and write, the machine is also easy to parse and generate, Json is a subset of JavaScript.

It simply is json javascript objects and arrays, so both objects and data structures is the two structures, two structures can be represented by a variety of complex structures

The python json serialization module and deserialization process are encoding and decoding.

encoding: converting a python Json string encoded as objects.
decoding: converting the format string encoded into python json object.
  json provides four functions: json.loads json.dumps json.load json.dump. dumps is used to handle loads with a string, load with dump is used to process the file.

       dumps: converting other objects or json format as an example format:

import json
mydict={'zhen':1,'to':'2'}
print type(mydict)
print type(json.dumps(mydict))
myjson=json.dumps(mydict)

       loads: json converted into a dictionary dict, the list into json example:

json Import 
mydict1 = json.loads (myjson) 
Print type (mydict1) 
mylist = '[1,2,3,4]'
json.loads (mylist) # json into the List
type (mylist)

       dump: writes the data to a file json

import json
jsondata = '''{"a":1,"b":2,"c":3}'''
with open('tmp.txt','w') as f:
    json.dump(jsondata,f)
    

  load: the contents of the file data is converted into json

import json
with open('tmp.txt','r') as f:
    json.load(f)

  

Guess you like

Origin www.cnblogs.com/zidonghuaqianxing/p/11718457.html