A simple introduction to JSON
Structurally, all the data can eventually be divided into three categories:
The first type is scalar (scalar), which is a separate string (string) or number (numbers), such as the single word "Beijing".
The second type is sequence (sequence), in which several related data are tied together in a certain order, also known as an array or list, such as "Beijing, Tokyo".
The third type is mapping (map), a name/value pair (Name/value), that is, the data has a name, and a corresponding value, which is also known as hash (hash) or dictionary (dictionary), such as "Capital: Beijing".
JSON (JavaScript Object Notation) is a lightweight data interchange format whose rules are simple and interesting:
1) The data is separated by commas (",").
2) The map is represented by a colon (":").
3) a set (array) of side data is expressed in square brackets ("[]").
4) The Set of Mappings (objects) are represented by curly braces ("{}").
According to this rule, the following understandings can be made:
1. Arrays are created with "[]", objects are created with "{}", and using JSON is basically an array or object created with [] or {}, otherwise an ordinary string is meaningless;
2. Whether it is an array or an object, the elements are separated by ",";
3. Inside the object, the name and value of the (attribute) are separated by ":" and must be separated by ":", and the property name or value cannot exist separately;
4. Objects and arrays can be nested, that is, an element in an array can be an object or an array, and the value of an attribute in the same object can be an object or an array.
Ii. examples
1.
var china= {beijing:{name: "Beijing", Area: "16000", Haidian:{name: "Haidian"}},
Shanghai:{name: "Shanghai", Area: "10000", Minhang:{name: "Minhang"}};
alert (china.beijing.haidian.name);
alert (china.shanghai.minhang.name);
"Haidian" and "Minhang" are ejected respectively.
2.
var ourcountry=[["Beijing"],["Shanghai"],["Hefei", "Wuhu", "Bangbu"];
Alert (ourcountry[2][1]);
Eject "Wuhu".
3.
var zhongguo={provinces:[{name: "Beijing", Cities:[{name: "Peking", quxian:["Haidian District", "Chaoyang District", "Dongcheng District", "Xicheng District"]},
{Name: "Anhui Province", Cities:[{name: "Wuhu", quxian:["Fanchang County", "Wuhu County", "Nanling County", "Three Mountain"]},{name: "Hefei", quxian:["Feixi County", "Shushan District", "Luyang District"]}]},
"Hubei Province"
]};
var str = "China: {\ n";
for (var i = 0; i < zhongguo.provinces.length; i++)
{
if (zhongguo.provinces.cities! = null)
{
str + = Zhongguo.provinces.name + "{";
for (var j = 0; J < Zhongguo.provinces.cities.length; J + +)
{
if (zhongguo.provinces.cities[j]! = NULL)
{
str + = Zhongguo.provinces.cities[j].name + "{";
for (var k = 0; k < zhongguo.provinces.cities[j].quxian.length; k++)
{
str + = Zhongguo.provinces.cities[j].quxian[k];
if (k! = zhongguo.provinces.cities[j].quxian.length-1)
{
str + = ",";
}
}
str + = "}";
}
}
str + = "}\n";
}
}
str + = "}";
alert (str);
Pop-up
China: {
Beijing {Haidian District, Chaoyang District, Dongcheng District, Xicheng District}}
Anhui Province {Wuhu {Fanchang County, Wuhu County, Nanling County, three mountains} Hefei {Feixi County, Shushan District, Luyang District}}
}
”。
Three, the application of JSON in Ajax
The client can easily submit data to the server through the address bar or post, but after the server finishes processing the data, it is difficult to pass the computed result information back to the client, especially when the data volume is large. This time the format of the data is the key, according to a format can be very convenient to the assembly of data, and then can be very convenient to parse. Using JSON is a good strategy. On the server side, the good one string is assembled in JSON format, responding to the client. How does the client parse it? There are generally two strategies (the names of the two strategies are the names of their own, not necessarily very reasonable, but the idea should be no problem):
1. Direct parsing
var json = eval (' (' + result + ') ');
Through this expression, the server-side response to the client's JSON-formatted string is parsed into a JSON (formatted) object, named "JSON", through "JSON." or "json[]" to access the data.
2. Indirect analysis
var json = "r=" + result;
Eval (JSON);
Of course the above line of code can be combined as: eval ("r=" + result);
The above calculation can also parse the server-side response to the client's JSON-formatted string into a JSON (formatted) object, but the object name is "R", through "R." or "r[]" to make data access.
Summary: JSON is a simple data interchange format that can be used to replace XML with a flexible exchange of data between servers.
Iv. arrays and objects in JavaScript
In JavaScript, the data format that is usually created with [] is called an array, and what is created with {} is called an object.
There is an array a=[1,2,3,4], there is an object A={0:1,1:2,2:3,3:4}, Run alert (a[1]), the results of the operation are the same in both cases! This means that the data set can be represented either as an array or as an object, so which one should be used?
In fact, arrays represent collections of ordered data, whereas objects represent collections of unordered data. If the order of the data is important, use an array, or you will use the object.
Of course, another difference between arrays and objects is that the data in the array has no name, and the data in the object has a name. But the problem is that in many programming languages, there is something called "Associative array" (Associativearray). The data in this array is named. In JavaScript, for example, you can define an object like this:
var a={"City": "Beijing", "area": 16800,Interesting, "Population": 1600};
However, it can also be defined as an associative array:
var a = new Array ();
a["City"]= "Beijing";
a["Area"]=16800;
a["Population"]=1600;
This makes it seem as if the array and the collection are not different, in fact, in the JavaScript language, the associative array is the object, and the object is the associative array. Arrays created in the second way are very different from arrays created by [], and in the "array" created in the second way can be similar to the first way, "a. City" to get "Beijing", "a. Population" to get "1600", But it and the first way to create a different object, the first way to define a is not the length property, two by the second way to define a has, but the value of 0, can be seen inside the difference or some, to be specific, I am afraid to look at the bottom of some implementation code.
When Eval returns a JSON value, it prompts Error:invalid Label Summary: We often return JSON-formatted values on the server side, which can be used directly in the script as a complete object, but many novices often encounter an error message: Invalid Lab El, this problem is usually distressing because the correct JSON format is clearly incorrect. If you're having this problem, take a quick look at the content of this article!
We often return JSON-formatted values on the server side, which can be used directly in the script as a complete object, but many novices often encounter an error: Invalid Label, this problem is often distressing, because the correct JSON format, but the error is indicated.
If you are returning a JSON-formatted string from the server side:
>
When you run in the script with eval:
>
This will prompt: Error:invalid Label
In fact, there is no error in our JSON format, except that when you eval, you enclose your JSON value in "()" brackets:
> var result = eval ("(" + O.responsetext + ")"); help001
Go to JSON Array object and object array