Currently, JQuery is almost used for project data interaction, so the processing process is: front-end page data-JS object-jQuery submission-python processing, and the other is reverse processing. Python certainly cannot directly process JS object data, so it is necessary to convert JS objects into a data format (usually dictionary dict) that python can process. Similarly, when python obtains data feedback to the front-end, it also needs to convert Dictionary data into objects that can be processed by JS. The intermediate conversion data format is usually JSON.
1. Convert JS objects to JSON
Process: Read the front-end page data, assemble it into a JS object, and pass it to python through jQuery's $. post () method.
Processing: reference A json2.js file and call the JSON. stringify () method. For example: var data = new Object (); var json_data = JSON. stringify (data );
Read: python is easy here. It's okay to use dict_data = json. loads (json_data ).
Ii. Convert JSON into JS
Process: python assembles a dict data and transfers it to the front end in JSON format, or the front end directly reads the data in JSON format using the $. getJSON () method of jQuery.
Processing: Use jQuery's method $. parseJSON () to convert JSON-format data into JS objects. For example: var json_data = $. getJSON (); var data = $. parseJSON (json_data );
Reading: javascript image operations are unnecessary.
Here, python needs to convert the dictionary into JSON format data, just use the json. dumps () method.
PS: The json2.js file can be downloaded after being searched online.