json json string objects and what difference ah

Object attributes can be accessed by json JavaScript! json json string objects loaded into the transmission data is often used for the front and back!


If you are using in the foreground, then Json objects can be invoked by xx.name, if it is a string, then the string is


ajax, we own stitching is a JSON object, thought it was no data type, so the default JS fact, the object according to its format,

If you send back stage, we first installed it into JSON characters.

 

From ajax sent a server, it must be a string, you want to resolve it, very simple, it becomes a JSON object to the job.

 

so amazing.

 

In the data transmission process, JSON as text, i.e., the character string in the form of transfer, and the operation is JS JSON object, so that the conversion between JSON object and JSON string key. For example:
the JSON string:
var str1 = '{ "name": "CXH", "Sex": "man"}'; 
the JSON objects:
var str2 = { "name": "CXH", "Sex": "man "};


A, JSON string into a JSON object

To use the above str1, you use the following method to be converted to JSON object:

// string into a JSON object JSON

var obj = Avl ( '(' + Str + ')');

or

var obj = str.parseJSON (); // Converts a string to a JSON object JSON

or

var obj = JSON.parse (str); // Converts a string to a JSON object JSON

Then, you can read this:

Alert(obj.name);

Alert(obj.sex);

Special Note: If obj has always been a JSON object, then use eval () function after conversion (even multiple conversions) or JSON object, but using parseJSON () function will have problems after treatment (throws grammatical anomaly).



Second, you can use the toJSONString () method of the JSON.stringify or global () object into a JSON JSON string.

E.g:

var last = obj.toJSONString (); // JSON object into the character JSON

or

var last = JSON.stringify (obj); // JSON object into the character JSON

alert(last);

 

Guess you like

Origin www.cnblogs.com/laopo/p/11241633.html