Detailed introduction to json objects in js
1. JSON (JavaScript Object Notation) is a simple data format, which is lighter than xml. JSON is a JavaScript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
JSON rules are simple: the object is an unordered set of "'name: value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.
The rules are as follows:
1) The ing is represented by a colon. Name: Value
2) The parallel data is separated by commas. Name 1: value 1, name 2: Value 2
3) The ing set (object) is represented by braces. {Name 1: value 1, name 2: Value 2}
4) The set (array) of the parallel data is represented by square brackets.
[
{Name 1: value, name 2: Value 2 },
{Name 1: value, name 2: Value 2}
]
5) Types of element values: string, number, object, array, true, false, null
2. Five methods in json:
1) traditional data storage and calling data
The Code is as follows:
<Script type = "text/javascript">
// Define "class" in the traditional JS Mode"
Function Person (id, name, age ){
This. id = id;
This. name = name;
This. age = age;
}
// Create an "object" in the traditional JS Mode"
Var p = new Person (20141028, "one leaf fl", 22 );
// Call the attributes in the class to display the Person information
Window. alert (p. id );
Window. alert (p. name );
Window. alert (p. age );
</Script>
2) first style:
The Code is as follows:
<Script type = "text/javascript">
Var person = {
Id: 001,
Name: "yiye Binzhou ",
Age: 23
}
Window. alert ("No.:" + person. id );
Window. alert ("username:" + person. name );
Window. alert ("age:" + person. age );
</Script>
3) The second style:
<Script type = "text/javascript">
Var p = [
{Id: 001, name: "yiye Binzhou", age: 22 },
{Id: 002, name: "No regrets", age: 23 },
{Id: 003, name: "No regrets _ one leaf fl", age: 24}
];
For (var I = 0; I <p. length; I ++ ){
Window. alert ("No.:" + p [I]. id );
Window. alert ("username:" + p [I]. name );
Window. alert ("age:" + p [I]. age );
}
</Script>
4) Third style:
The Code is as follows:
<Script type = "text/javascript">
Var p = {
"Province ":[
{"City": "Fuzhou "},
{"City": "Xiamen "},
{"City": "Putian "}
]
};
Window. alert ("city:" + p. province [0]. city );
</Script>
5) Fourth style:
The Code is as follows:
<Script type = "text/javascript">
Var p = {
"Ids ":[
{"Id": 001 },
{"Id": 002 },
{"Id": 003}
],
"Names ":[
{"Name": "yiye Binzhou "},
{"Name": "No regrets "},
{"Name": "No regrets _ one leaf fl "}
]
};
For (var I = 0; I <p. names. length; I ++ ){
Window. alert ("name:" + p. names [I]. name );
}
For (var I = 0; I <p. ids. length; I ++ ){
Window. alert ("id:" + p. ids [I]. id );
}
</Script>
6) Fifth style:
The Code is as follows:
<Script type = "text/javascript">
Var p = {
"Province": ["Fuzhou", "Xiamen", "Putian"]
};
Window. alert ("number of cities:" + p. province. length );
Window. alert ("respectively: \ n ");
For (var I = 0; I <p. province. length; I ++ ){
Window. alert (p. province [I]);
}
</Script>