The current project data interaction is almost all with jquery, so the process is: Front page data-"JS Object-" jquery submission-"Python processing, the other is reversed. Python must not directly handle the JS object data, so to convert the JS object into a python can handle a data format (usually a dictionary dict), the same, Python data feedback to the front-end also to convert the dictionary data to JS can handle the object, This intermediate conversion data format is usually JSON.
One, JS object converted into JSON
Process: Read the front page data, assemble it as a JS object, and pass it to Python via the $.post () method of jquery.
Processing: Referencing a json2.js file, calling the Json.stringify () method. For example: var data = new Object (); var json_data = json.stringify (data);
READ: Python here is very simple, with dict_data = Json.loads (json_data) is OK
Second, JSON conversion into JS
Process: Python assembles a dict data into JSON format and passes it to the front end, or the front end reads the JSON-formatted data directly through the jquery $.getjson () method
Processing: A method of jquery $.parsejson () converts data in JSON format into a JS object. For example: var json_data = $.getjson (); var data = $.parsejson (Json_data);
READ: JS to the operation of the image do not have to say more
Here, Python is going to convert the dictionary into JSON-formatted data, using the Json.dumps () method.
PS:json2.js This file can be downloaded by searching the Internet.
Conversion of JS object to JSON format