The relationship and transformation between Json and JS objects

Source: Internet
Author: User
Tags string to json

background

The relationship between a JavaScript object and a JSON object, JSON is a subset of JavaScript. JavaScript objects are a representation of object-oriented languages, and JSON is a form of data transfer (such as XML), so there is no significant relationship between the two definitions. But the format of the two is very similar (one of the details is: JS object key can use quotation marks can not be used, but the JSON format must use quotation marks for the key ), so the JS object and JSON is very easy to convert, but it must be remembered that In the JavaScript language JSON is just a fixed-format string, just a string!


1 Convert a JSON string to a JavaScript object using the Eval method that comes with JavaScript

var obj = eval (' (' +jsonstr + ') ');

It is a good idea to use parentheses because the parentheses force the outermost curly brace of the JSON string to be defined as the semantics of the object's direct amount, because the curly braces in JavaScript have a lot of semantics. In contrast, the parentheses we use (function () {}) () are the braces that understand the definition of a function, so this function does


2 Using the Advanced browser's own parsing mechanism

Advanced browsers such as FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8 can directly use the stringify () and Parse () methods of JSON objects.

Json.stringify (obj) converts the JSON to a string.

Json.parse (String) to convert the string to JSON format;


3 The JSON method supported by JQuery

var obj = Jquery.parsejson (' {' name ': ' John '} ');

4 JSON methods supported by ExtJS

var jsonstr = Ext.util.JSON.encode (obj);

var obj = Ext.util.JSON.decode (JSONSTR);

In addition, encode and Decode are assigned to Ext.encode and Ext.decode respectively.

That is, you can use shorter ext.encode and ext.decode rather than lengthy Ext.util.JSON.encode and Ext.util.JSON.decode for development.

1 ext.encode = Ext.util.JSON.encode;

2 Ext.decode = Ext.util.JSON.decode;

That is, you can use shorter ext.encode and ext.decode rather than lengthy Ext.util.JSON.encode and Ext.util.JSON.decode for development.


The relationship and transformation between Json and JS objects

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.