During js development, we often encounter the mutual conversion between JS objects and JSON-format data d. This article introduces the mutual conversion between js objects and json objects, for more information, see.
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.
Instance
Var data = new ob ject (); 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.
Instance:
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.