We know that json and js Arrays can be converted to each other. Next I will introduce the differences between arrays and json. Some friends may not know about them at all, in fact, they are different from each other.
I. Array
1. Define a one-dimensional Array: var s1 = new Array ();
The Code is as follows: |
Copy code |
S1 = [1, 2, 4] or s1 [0] = 1, s1 [1] = 2, s1 [3] = 3, s1 [4] = 4; |
Alert (s1 [0]);
The result is 1;
The Code is as follows: |
Copy code |
2. Define a two-dimensional prime group: Var s1 = new Array (); Var s1 = [[], [, 4], 3, [, 8]; Alert (s1 [1] [0]); |
The result is 2;
2. Define a json object
1. json object
The Code is as follows: |
Copy code |
Var status_process = { "Name5": 'idle time ', "Name1": 'seeding time ', "Name2": 'seedling stage ', "Name3": 'duration ', "Name4": 'collection period' } Alert (status_process ); The result is: Object; |
2, json string
Json string indicates that the value of the string variable is in the same format as that of json, but is not a json object. For example:
The Code is as follows: |
Copy code |
Var s1 = "{"; Var s2 = "'name5': 'freeze period ', 'name1': 'seeding period', 'name2': 'seedling period ', 'name3': 'growth period', 'name4 ': 'collection period '"; Var s3 = "}"; Var status_process = s1 + s2 + s3; |
Although the value of status_process conforms to the json object format, it is not an object but a string (pieced together );
Convert a string to a json object using the function eval, eval ("(" + status_process + ")");
Conclusion: The json string passed in from the background to the foreground is not a real json object. Therefore, the eval function must be used for conversion.
3. Use of json objects
The Code is as follows: |
Copy code |
Var status_process = { Name5: 'freetier id ', Name1: 'seeding instance ', Name2: 'seedling file ', Name3: 'duration ', Name4: 'collection expires' }; Alert (status_process ["name5"]); Alert (status_process.name5 ); |
Both are: idle period
4. Two-Dimensional json object
The Code is as follows: |
Copy code |
Var status_process = { Name5: {name3: 'idle idleness '}, Name1: 'seeding instance ', Name2: 'seedling file ', Name3: 'duration ', Name4: 'collection expires' }; Alert (status_process ["name5"] ["name3"]); Alert (status_process.name5.name3 ); |
The result is: 'idle idleness'
JSON (JavaScript Object Natation) is a lightweight data exchange format. It is based on a subset of javascript syntax, that is, array and Object representation.
Javascrip data type, object and array:
Javascript data structures mainly include the following four basic data types: string, number, boolean, null, and js. They also contain complex data structures, that is, objects. We can regard the array as a special object. Objects and groups can all contain different types, including objects and arrays.
The js object is defined as follows:
The Code is as follows: |
Copy code |
Var boy = { "Name": "xiaoming ", "Age": 4, "City": "Hangzhou" "HasBrother": true }; |
Objects can be viewed as containers with many key-value pairs installed, similar to Map <String, Object> in java. We can obtain the value in this way.
The Code is as follows: |
Copy code |
Alert ("name =" + boy ["name"]); Alert ("age =" + boy. age ); |
The biggest difference between arrays and objects is that arrays are ordered, instead of using keys to obtain values, but indexing through indexes. The object has more length attributes.
The Code is as follows: |
Copy code |
Var array = ["hello", 12, true, null]; Alert (array [0]); |
Of course, objects and arrays can be mixed. The following simple mixture can be more complex.
The Code is as follows: |
Copy code |
Var array = ["hello", 12, true, {"name": "xiaoming", "age": 4}]; Var boy = {"name": "xiaoming", "brothers": ["xiaohuang", "xiaohong"]}; |
Note that we can define
The Code is as follows: |
Copy code |
Var a = [1, 2, 3]; // defines an array Alert (a. length); // The result is 3. A ["name"] = "xiaoming"; // We will assign a value to it. Alert (a. length); // The result is 3. A. push (4 ); Alert (a. length); // The result is 4 |
The above code a ["name"] = "xiaoming" adds an attribute name to array a, instead of adding data to the array.
In fact, we can think of arrays in this format, that is, Special Objects (the type attribute is written at will ,)
The Code is as follows: |
Copy code |
Var array = { "Type": "array ", "Value": [1, 2, 3], "Length": 3 }; |
After a ["name"] = "xiaoming"; is added, the array becomes like this and does not affect the value of the array.
The Code is as follows: |
Copy code |
Array = { "Type": "array ", "Name": "xiaoming ", "Value": [1, 2, 3], "Length": 3 }; |
Last look at an instance
The Code is as follows: |
Copy code |
Function arraytojson (o ){ Var r = []; If (typeof o = "string") return "+ o. replace (/(['"])/g," $1 "). replace (/(n)/g, "n "). replace (/(r)/g, "r "). replace (/(t)/g, "t") + """; If (typeof o = "object "){ If (! O. sort ){ For (var I in o) R. push (I + ":" + arraytojson (o [I]); If (!! Document. all &&! /^ N? Functions * tostring () s * {n? S * [native code] n? S *} n? S * $/. test (o. tostring )){ R. push ("tostring:" + o. tostring. tostring ()); } R = "{" + r. join () + "}"; } Else { For (var I = 0; I <o. length; I ++ ){ R. push (arraytojson (o [I]); } R = "[" + r. join () + "]"; } Return r; } Return o. tostring (); }
|
For more details, see http://www.bKjia. c0m/wy/js-ajax/array-json.htm