This article transferred from: http://www.cnblogs.com/csj222/archive/2013/04/11/3013667.html
For the front end is completely rookie, forced to work in the use of JS, especially the processing of JSON for more, on-line search, the basic similarity. So the usual use of more JSON processing method summed up a bit, the right when deepening memory.
I. Overview
JSON (JavaScript Object Notation) is a lightweight data interchange format that is ideal for data interchange formats in a completely language-independent text format. At the same time, JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.
In JSON, there are two kinds of structures: objects and arrays.
1. Objects
An object ends with "{" Starting with "}". Each "key" followed by a ":", "' Key/value ' pair" is applied "," separated.
Packjson = {"Name": "Nikita", "Password": "1111"}
2. Arrays
Packjson = [{"Name": "Nikita", "Password": "1111"}, {"Name": "Tony", "Password": "2222"}];
An array is an ordered collection of values. An array ends with "[" Start, "]". Use "," to separate values.
Second, JSON objects, and JSON Conversion of Strings
In the data transfer process, JSON is passed in the form of text, which is a string, and JS operates on a JSON object, so the conversion between the JSON object and the JSON string is key. For example:
JSON string:
var jsonstr = ' {' name ': ' Nikita ', ' Password ': ' 1111 '} ';
JSON object:
var jsonobj = {"Name": "Nikita", "Password": "1111"};
For the front end is completely rookie, forced to work in the use of JS, especially the processing of JSON for more, on-line search, the basic similarity. So the usual use of more JSON processing method summed up a bit, the right when deepening memory.
I. Overview
JSON (JavaScript Object Notation) is a lightweight data interchange format that is ideal for data interchange formats in a completely language-independent text format. At the same time, JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.
In JSON, there are two kinds of structures: objects and arrays.
1. Objects
An object ends with "{" Starting with "}". Each "key" followed by a ":", "' Key/value ' pair" is applied "," separated.
Packjson = {"Name": "Nikita", "Password": "1111"}
2. Arrays
Packjson = [{"Name": "Nikita", "Password": "1111"}, {"Name": "Tony", "Password": "2222"}];
An array is an ordered collection of values. An array ends with "[" Start, "]". Use "," to separate values.
Second, JSON objects, and JSON Conversion of Strings
In the data transfer process, JSON is passed in the form of text, which is a string, and JS operates on a JSON object, so the conversion between the JSON object and the JSON string is key. For example:
JSON string:
var jsonstr = ' {' name ': ' Nikita ', ' Password ': ' 1111 '} ';
JSON object:
var jsonobj = {"Name": "Nikita", "Password": "1111"};
2. Convert string to JSON
Eval is a function of JS, not very safe, you can consider the JSON package.
Third, Traverse JSON Object
Myjson = {"Name": "Nikita", "Password": "1111"};for (var p in Myjson) {//traverse each key/value pair of JSON object, p is key alert (P + "" + Myjs On[p]);}
Operation Result:
Four, Traverse JSON Array
Packjson = [{"Name": "Nikita", "Password": "1111"},{"name": "Tony", "Password": "2222"}];for (var p in Packjson) {// When traversing a JSON array, this writes p for the index, 0,1 alert (packjson[p].name + "" + Packjson[p].password);}
I'm more inclined to do this:
for (var i = 0; i < packjson.length; i++) { alert (packjson[i].name + "+ Packjson[i].password);}
Operation Result:
Five, the two JSON object assembled into one inside
Targetjson Target Json,packjson is assembled jsonfunction Addgroupjson (Targetjson, Packjson) { if (Targetjson && Packjson) {for (Var p in Packjson) { targetjson[p] = Packjson[p];}} }
Use the following:
var json1 = {"Name": "Nikita"};var json2 = {"Password": "1111"};addgroupjson (Json1, Json2); alert (Json2str (Json1));
Operation Result:
Go A summary of the operation of JS on JSON