JS arrays How to add JSON data and the difference between JS array and JSON _javascript tips

Source: Internet
Author: User

JSON (JavaScript Object notation) is a lightweight data interchange format that takes a completely language-independent text format, and JSON is a JavaScript native data format.

Here are two ways of adding JSON data to JS arrays.

The first way

Personinfo
: [], for
(var i = 0; i < _stage.passengerinfoarray.length; i++) {
var name = _stage.passengerinf Oarray[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 JS arrays and JSON

One, array

1. Define 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 was 1;

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

 var status_process = {
       "name5": ' Idle Period ', '
     name1 ': ' Sowing time ',
     ' name2 ': ' Seedling ', ' name3 ': '
     growing season ', '
     name4 ': ' Harvesting period '
    }    
   alert (status_process);

The result is: 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:

       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

var status_process = {
      name5: ' Idle period ',
     name1: ' Sowing time ',
     name2: ' Seedling ', Name3: '
      growing period ',
      name4: ' Harvest '
     };
     alert (status_process["Name5"]);
     alert (STATUS_PROCESS.NAME5);

Two are: Idle period

4,json Two-dimensional object

var status_process = {
 name5: {name3: ' Idle idle Period '}, name1: ' Sowing time ', name2: ' Seedling stage ', Name3: '
 growing period
 ',
 name4: ' Mining Receiving period '
};
Alert (status_process["Name5"] ["Name3"]);
alert (STATUS_PROCESS.NAME5.NAME3);

The results are: ' Idle idle period '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.