How to add json data to js arrays and the difference between js arrays and json, jsjson

Source: Internet
Author: User

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'

Related Article

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.