Conversion of JS object to JSON format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.