Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Create JSON data directly in JS and traverse it


May 08, 2021 JSON


Table of contents


Create the JSON data directly in JS, and then traverse the usage
The creation code is as follows: (JSON object is created)

var YearSelect = {}; 
var Year = 2014; 
var DateOption; 
for (var i = Year; i < Year + 12; i++) { 
DateOption = {'Year':i, 'Month':i-Year+1}; 
/ alert(DateOption.Year) 
YearSelect[i] = DateOption; 
}

Here is to create a JSON object that includes data for the year and month.
Why I created the JSON object is because I am familiar with the JSON object. The php background also returns the johnson object.
The json object does not have the legth attribute
So traversing the words to:

for(var key in YearSelect){ 
alert(YearSelect[key].Year); 
alert(YearSelect[key].Month); 
}

That's it
Remember to distinguish between jason's objects and arrays - otherwise it's always unundefined

How the js array adds the jason data and the difference between the js array and the json

Here are two ways to add jason data to a js array.
The first way

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);
}

The second way

var passengerInfo = {};
passengerInfo.psgTypeDesc = psgTypeDesc;
passengerInfo.flightPrice = flightPrice;
_STAGE.passengerInfoArray.push(passengerInfo);

The difference between the js array and json

One, array

1. Define a one-dimensional array: var s1 s new Array();
s1, 2, 3, 4, or s1, 0, s1, 1, 2, s1, 3, s1, 3, s1, 4, 4;
alert(s1[0]);
The result is 1;

2. Define the two-dimensional group: var s1 s new Array();
var s1=[[3,1],[2,3,4],3,[4,5,6,7,8]];
alert(s1[1][0]);
The result is 2;

Second, define the json object

1. json object

var status_process = {
    " name5" : '闲置期',
    "name1" : '播种期',
    "name2" : '苗期',
    "name3" : '生长期',
    "name4" : '采收期'
   }    
  alert(status_process);

The result is: Object:Object;


2. json string

The so-called jason string means that the value of the string variable is the same as the json's format, but not the johnson object, for example:

var s1="{";
var s2 = " 'name5' : '闲置期',  'name1' : '播种期','name2' : '苗期','name3' : '生长期','name4' : '采收期'";
var s3="}";
var status_process=s1+s2 +s3;

Although status_process the value of the object conforms to the format of the jason object, it is not an object, it is just a string (it is pieced together);

Convert the string to a jason object using the function eval, eval (" status_process """

Conclusion: From the background to the fore desk is the jason string, not the real jason object, so you need to use the eval function conversion.

3. Use of json objects

var status_process = {
      name5 : '闲置期',
     name1 : '播种期',
     name2 : '苗期',
      name3 : '生长期',
      name4 : '采收期'
     };
     alert(status_process["name5"]);
     alert(status_process.name5);

Both are: idle periods

4. json two-dimensional object

var status_process = {
 name5 : {name3:'空闲闲置期'},
 name1 : '播种期',
 name2 : '苗期',
 name3 : '生长期',
 name4 : '采收期'
};
alert(status_process["name5"]["name3"]);
alert(status_process.name5.name3);

The result is: 'Idle idle period'