One, array
1. Define one-dimensional array: var s1=new array ();
The code is as follows |
Copy Code |
s1=[1,2,3,4] or s1[0]=1,s1[1]=2,s1[3]=3,s1[4]=4; |
Alert (s1[0]);
The result was 1;
The code is as follows |
Copy Code |
2, define the two-dimensional group: var s1=new Array (); var s1=[[3,1],[2,3,4],3,[4,5,6,7,8]]; Alert (s1[1][0]); |
The result was 2;
Second, define the JSON object
1,json objects
code is as follows |
copy code |
var status_process = { "name5": ' Idle period ', "name1": ' Sowing Date ', "name2": ' Seedling ', ' name3 ': ' Growing period ', "name4": ' Harvest Time ' }      &NBSP alert (status_process); results are: object:object; |
2,json string
A JSON string, which means that the value of the string variable is the same as the JSON format, but not a JSON object, such as:
The code is as follows |
Copy Code |
var s1= "{"; var s2 = "' name5 ': ' Idle period ', ' name1 ': ' Sowing time ', ' name2 ': ' Seedling ', ' name3 ': ' Growth period ', ' name4 ': ' Harvest '"; var s3= "}"; var status_process=s1+s2 +s3; |
Although the value of status_process conforms to the format of the JSON object, it is not an object, just a string (is pieced together);
Converts a string to a JSON object using the function Eval,eval ("(" + status_process+ ")");
Conclusion: The JSON string, not the true JSON object, is passed into the foreground from the background, and therefore needs to be converted using the Eval function.
Use of 3,json objects
The code is as follows |
Copy Code |
var status_process = { NAME5: ' Idle period ', NAME1: ' Sowing date ', Name2: ' Seedling ', Name3: ' Growing period ', Name4: ' Harvest Time ' }; Alert (status_process["Name5"]); alert (STATUS_PROCESS.NAME5); |
Two are: Idle period
4,json Two-dimensional object
The code is as follows |
Copy Code |
var status_process = { Name5: {name3: ' Idle idle Period '}, NAME1: ' Sowing date ', Name2: ' Seedling ', Name3: ' Growing period ', Name4: ' Harvest Time ' }; Alert (status_process["Name5"] ["Name3"]); alert (STATUS_PROCESS.NAME5.NAME3); |
The results are: ' Idle idle period '
About JSON (JavaScript Object natation) is a lightweight format for exchanging data, based on a subset of JavaScript syntax, arrays and object representations.
Javascrip data types, objects and arrays:
JavaScript data structure mainly has the following four kinds of basic data types, STRING,NUMBER,BOOLEAN,NULL,JS also has the complex data structure, namely the object. We can think of arrays as a special object. Objects and arrays can 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 considered containers, which contain many key-value pairs, similar to those in Java map<string,object>. We can get a value like this
The code is as follows |
Copy Code |
Alert ("name =" + boy["name"]); Alert ("age=" + boy.age); |
And the array can be the largest difference with the object is that the array is ordered, not the way to get the value of the key, but indexed by index, relative to the object is more than the length attribute.
The code is as follows |
Copy Code |
var array=["Hello", A, true, NULL]; Alert (array[0]); |
Of course, objects and arrays can be mixed, and the following are simply mixed, which can be more complex, of course.
The code is as follows |
Copy Code |
var array=["Hello", A, True, {"name": "Xiaoming", "Age": 4}]; var boy= {"Name": "Xiaoming", "Brothers": ["Xiaohuang", "Xiaohong"]}; |
It should be noted that in peacetime we can define
The code is as follows |
Copy Code |
var a = [1,2,3]; Define an array alert (a.length); Result is 3 a["name"] = "xiaoming"; We're going to give it another assignment. alert (a.length); The result is still 3. A.push (4); alert (a.length); Result is 4 |
The above code a["name"]= "Xiaoming" to add a property called Name to array a, rather than adding data to the array.
You can actually think of an array in this format, which is a special object (the attribute of type is casually written)
The code is as follows |
Copy Code |
var array= { ' Type ': ' Array ', "Value": [1,2,3], "Length": 3 };
|
Plus a["name"] = "xiaoming"; After that, the array becomes like this and does not affect the value of the array itself.
The code is as follows |
Copy Code |
array= { ' Type ': ' Array ', "Name": "Xiaoming", "Value": [1,2,3], "Length": 3 }; |
Finally look at an example
code is as follows |
copy code |
function Arraytojson (o) { var r = []; if (typeof o = = "string") return "" "+ o.replace (/[["])/g, "$". 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.111cn.net/wy/js-ajax/array-json.htm