Use Javascript/jQuery to convert javascript objects to json format data

Source: Internet
Author: User

The eval () function provided by Javascript can convert json data into Javascript objects. However, Javascript objects are not serialized as functions in json format. [Javascript] var json = '{"name": "John"}'; var obj = eval ('+ json +'); alert (obj. name); $. parseJSON (json) is used to convert json into Javascript objects, but does not provide a function to convert Javascript objects into json. [Javascript] var json = {"name": "John"}; var obj = $. parseJSON (json); alert (obj. name); what if we want to convert the Javascript Object into json and send it to the server? It is actually very simple. You can use the jquery. json plug-in. The current version is as follows: jquery. json-2.4.js (compressed version: jquery. json-2.4.min.js) the plug-in provides four functions for parsing and anti-parsing json, specifically: • toJSON: converts javascript object, number, string, or array into JSON data. • EvalJSON: converts JSON-format data into Javascript objects at a very fast speed, but this speed is negligible (as mentioned by the author ). • SecureEvalJSON: converts JSON to a Javascript Object, but checks whether the converted data is in JSON format before conversion. • QuoteString: Add quotation marks at both ends of the string and intelligently escape (skip) any quotation marks, backslash, or control characters. Example (Be sure to use it with the jQuery core library): [javascript] var json = {plugin: 'jquery-json', version: 2.4 }; // '{"plugin": "jquery-json", "version": 2.4}' var encoded = $. toJSON (json); // get the name value: "jquery-json" var name = $. evalJSON (encoded ). plugin; // The value of version is 2.4 var version = $. evalJSON (encoded ). version;

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.