Python--Json data encoding and parsingJson Simple Introduction
JSON: JAvaScript Object Notation (JavaScript object notation)
JSON is the syntax for storing and exchanging textual information. Similar to XML.
JSON is smaller, faster, and easier to parse than XML.
This sites object is an array that contains 3 site records (objects).
{"sites""name": "Rookie Tutorial", "url": "Www.runoob.com""name": "Google", "url": "Www.google.com" "name": "Weibo", "url": "Www.weibo.com" }]}
JSON numbers
The JSON numbers can be integer or floating-point types:
{"Age": 30}
JSON Object
The JSON object is written in curly braces ({}):
An object can contain multiple name/value pairs:
{ "name":" rookie tutorial " , "url":"www.runoob.com " }
This is also easy to understand, and is equivalent to this JAVASCRIPT statement:
name = " Rookie tutorial " url = "www.runoob.com" JSON Array
The JSON array is written in brackets:
An array can contain multiple objects:
"Sites""name": "Rookie Tutorial", "url": "Www.runoob.com""name": "Google", "url": "Www.google.com" "name": "Weibo", "url": "Www.weibo.com"}
}
In the example above, the object "sites" is an array that contains three objects. Each object represents a record of a Web site (name, URL).
JSON Boolean value
The JSON Boolean value can be true or false:
{"Flag":True }
JSON NULL
JSON can set a null value:
{"Runoob":null }
JSON uses JavaScript syntax
Because JSON uses JavaScript syntax, there is no need for additional software to handle JSON in JavaScript.
With JavaScript, you can create an array of objects and assign values like this:
Instance
var sites ="name": "Runoob", "url": "Www.runoob.com""name": "Google", "url": " www.google.com "" name ":" Weibo "," url ":" Www.weibo.com " }];
You can access the first item in the JavaScript object array like this (the index starts at 0):
Sites[0].name;
The returned content is:
Runoob
You can modify the data like this:
Sites[0].name= "Rookie Tutorial";
Using the JSON library: import JSON json.dumps
The json.dumps is used to encode a Python object into a JSON string.
Grammar
# json.dumps (obj, 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)
#Indent Indent indent = 4 means indent 4 cells#Sort_keys=true Press keys to sort the show#Separators Separator separators= (', ', ': '), if the indentation has been used, then this can not be used, superfluous, the same effect. Json1=json.dumps (data)PrintJson1#[{"A": 1, "C": 3, "B": 2, "E": 5, "D": 4}]Json2= Json.dumps (data,sort_keys=True)PrintJson2#[{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}]Json3= Json.dumps (data,sort_keys=False)PrintJson3#[{"A": 1, "C": 3, "B": 2, "E": 5, "D": 4}]TestData=[{'name': U'Stephen Chow',' Age': 28}]jsond=json.dumps (testData)PrintJsondPrintJson.dumps ({'a':'Runoob','b': 7}, Sort_keys=true, indent=4, separators= (',',': ')) #输出结果" "{"A": "Runoob", "B": 7}" "
Python primitive type conversion table to JSON type:
Json.loads
Grammar
# Grammar # json.loads (s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw] ]]'{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}'= json.loads ( Jsondata)print testD #{u ' a ': 1, U ' C ': 3, U ' B ': 2, U ' E ': 5, U ' d ': 4}
JSON type conversion to Python type comparison table:
Using third-party libraries: Demjson
Demjson is a third-party module library of Python that can be used to encode and decode JSON data, including the Jsonlint formatting and validation functions.
Installation
Method One:
: https://pypi.python.org/pypi/demjson/2.2.4
Switch to directory to execute the following command
python setup. PY Install
Method Two:
Execute command
Pip Install Demjson
After successful, the corresponding files and folders will be generated in site-packages.
JSON functions
Encode
The Python encode () function encodes a Python object into a JSON string.
Grammar
#-*-coding:utf-8-*-#TIME:2017/9/22 21:25#Author:yangyangjunImportDemjsondata= [ {'a': 1,'b': 2,'C': 3,'D': 4,'e': 5 } ]#Grammar#Demjson.encode (self, obj, nest_level=0)JSON =demjson.encode (data)PrintJson#[{"A": 1, "B": 2, "C": 3, "D": 4, "E": 5}]
Decode
Python can use the Demjson.decode () function to decode JSON data. The function returns the data type of the Python field.
Grammar
' {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5} ' ; # Grammar # Demjson.decode (self, txt)text = Demjson.decode (JSON)print text # {u ' a ': 1, U ' C ': 3, U ' B ': 2, U ' E ': 5, U ' d ': 4}
Python--Json data encoding and parsing