Conversion between js objects and json objects

Source: Internet
Author: User
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.

Related Article

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.