Home>

json (javascript object notation) is a lightweight data exchange format,In a language-independent text format,json is a javascript native data format.

Here are two ways to add json data to the js array.

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

//second way

var passengerinfo=();
passengerinfo.psgtypedesc=psgtypedesc;
passengerinfo.flightprice=flightprice;
_stage.passengerinfoarray.push (passengerinfo);

Difference between js array and json

First, the array

1. Define a one-dimensional array:var s1=new array ();

s1=[1,2,3,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=[[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":"Idle Period",     "name1":"Sowing date",     "name2":"Seedling stage",     "name3":"Growth Period",     "name4":"Harvest Period"
    }
   alert (status_process);

The result is:object:object;

2, json string

The so-called json string means that the value of the string variable is the same as the format of json.But not a json object, such as:

      var s1="{";
       var s2="" name5 ":" idle period "," name1 ":" seeding period "," name2 ":" seedling period "," name3 ":" growth period "," name4 ":" harvest period "" ;
       var s3="}";
       var status_process=s1 + s2 + s3;

Although the value of status_process conforms to the format of the json object,But it is not an object,Just a string (cobbled together);

Convert a string to a json object using the function eval, eval ("(" + status_process + ")");

Conclusion:The json string passed from the background to the foreground is not a real json object, so it needs to be converted using the eval function.

3, the use of json objects

var status_process={
      name5:"Idle period",     name1:"seeding period",     name2:"Seedling stage",      name3:"Growth Period",      name4:"harvest period"
     };
     alert (status_process ["name5"]);
     alert (status_process.name5);

Both are:Idle periods

4, json two-dimensional object

var status_process={
 name5:{name3:"Idle period"}, name1:"seeding period", name2:"Seedling stage", name3:"Growth Period", name4:"harvest period"
};
alert (status_process ["name5"] ["name3"]);
alert (status_process.name5.name3);

The results are:"Idle period"

  • Previous TextView width is too large in Android programming, Drawable cannot be centered
  • Next Start Python programming with PyCharm and Visual Studio on Windows
  • Trends