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