First, what is JSON?
JSON is a lightweight format for data interchange
Second, Python's thinking of working with JSON
In fact, it is easy to understand the data into JSON format data and bar JSON format data parsing out
Third, the specific method of Python processing JSON
1, Json.dumps ()
The function can convert the simple data type (INT\FLOAT\STRING\TUPLE\LIST\DICT\UNICODE) to JSON format, the sample code is as follows:
Import Jsonsrc_data = {"Name": "Tacey", "age": +, "sex": "Male", "interst":("programing", "Reading")} #print repr (src_ Data) print Json.dumps (src_data)
The output is as follows:
{' Interst ':(' programing ', ' Reading '), ' age ': +, ' name ': ' Tacey ', ' sex ': ' Male '} {' interst ': ["programing", "Reading"], " Age ":", "" Name ":" Tacey "," Sex ": MAL"}
2, Json.loads ()
The function can convert the JSON data into a python simple data type, followed by the code above:
Json_data = Json.dumps (src_data) print json.loads (json_data) ["Name"]
Output Result:
Tacey
Iv. JSON processing in flask
Flask can also be useful in Python's own JSON module, but also practical flask in the jsonify, sample code is as follows:
From flask import flask, Jsonifyapp = Flask (__name__) Json_data = [{' Name ': ' Tacey ', ' age ': $, ' sex ': ' Male ', ' interst ': ("programing", "Reading")} {"Name": "Amber", "Age": $, "sex": "female", "interst":("food", "Dog")}] @app. Route ('/jsontest ', methods=[' GET ') ) def Get_json (): Return jsonify ({' JSON ': Json_data}) if __name__ = = ' __main__ ': App.run (debug=true)
"Note": Now is just a simple practical, some have not been involved, such as non-Python comes with standard data types, JSON complete division
Reference:
Http://www.cnblogs.com/vovlie/p/4178077.html
Http://www.cnblogs.com/coser/archive/2011/12/14/2287739.html
Python processing JSON