How to add json data to js arrays and the difference between js arrays and json, jsjson
JSON (JavaScript Object Notation) is a lightweight data exchange format, using a completely language-independent text format, JSON is the JavaScript native data format.
The following describes how to add json data to a js array.
// Method 1
personInfo: [],for(var i = 0; i < _STAGE.passengerInfoArray.length; i++){var name = _STAGE.passengerInfoArray[i];var person = {v:name, text:name};this.personInfo.push(person);}
// Method 2
var passengerInfo = {};passengerInfo.psgTypeDesc = psgTypeDesc;passengerInfo.flightPrice = flightPrice;_STAGE.passengerInfoArray.push(passengerInfo);
Differences between js array and json
I. Array
1. Define a one-dimensional Array: var s1 = new Array ();
S1 = [1, 2, 4] or s1 [0] = 1, s1 [1] = 2, s1 [3] = 3, s1 [4] = 4;
Alert (s1 [0]);
The result is 1;
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
Var status_process = {"name5": 'freeze period ', "name1": 'seeding period', "name2": 'seedling period ', "name3": 'growth period', "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:
Var s1 = "{"; var s2 = "'name5': 'freeze period ', 'name1': 'seeding period', 'name2': 'seedling period ', 'name3 ': 'growth phase ', 'name4': 'Harvest phase' "; 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
Var status_process = {name5: 'freeze period ', name1: 'seeding period', name2: 'seedling period ', name3: 'growth period', name4: 'Harvest period '}; alert (status_process ["name5"]); alert (status_process.name5 );
Both are: idle period
4. Two-Dimensional json object
Var status_process = {name5: {name3: 'idle idle idleness '}, name1: 'seeding times', name2: 'seedling status', name3: 'growth times', name4: 'harvesting times '}; alert (status_process ["name5"] ["name3"]); alert (status_process.name5.name3 );
The result is: 'idle idleness'