Differences between JS array and JSON

Source: Internet
Author: User
Document directory
  • Javascrip data type, object and array:

This article Reprinted from: http://www.cnblogs.com/linda586586/archive/2012/04/17/2454212.html

 

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": 'idle time ',
"Name1": 'seeding time ',
"Name2": 'seedling stage ',
"Name3": 'duration ',
"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 period', 'name4 ': 'collection period '";
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: 'freetier id ',
Name1: 'seeding instance ',
Name2: 'seedling file ',
Name3: 'duration ',
Name4: 'collection expires'
};
Alert (status_process ["name5"]);
Alert (status_process.name5 );

Both are: idle period

 

4. Two-Dimensional JSON object

VaR status_process = {
Name5: {name3: 'idle idleness '},
Name1: 'seeding instance ',
Name2: 'seedling file ',
Name3: 'duration ',
Name4: 'collection expires'
};
Alert (status_process ["name5"] ["name3"]);
Alert (status_process.name5.name3 );

The result is: 'idle idleness'

 

 

Another article: http://asyty.iteye.com/blog/1260933.

JSON (JavaScript Object Natation) is a lightweight data exchange format. It is based on a subset of JavaScript syntax, that is, array and object representation.

Javascrip data type, object and array:

Javascript data structures mainly include the following four basic data types: String, number, Boolean, null, and JS. They also contain complex data structures, that is, objects. We can regard the array as a special object. Objects and groups can all contain different types, including objects and arrays.

 

The JS object is defined as follows:

VaR boy = {"name": "Xiaoming", "Age": 4, "city": "Hangzhou" "hasbrother": true };

 

Objects can be viewed as containers with many key-value pairs installed, similar to map <string, Object> in Java. We can obtain the value in this way.

 

alert("name = " + boy["name"] );alert("age=" + boy.age);

 

The biggest difference between arrays and objects is that arrays are ordered, instead of using keys to obtain values, but indexing through indexes. The object has more length attributes.

 

var array=["hello", 12, true , null];alert(array[0]);

 

Of course, objects and arrays can be mixed. The following simple mixture can be more complex.

 

var array=["hello", 12, true , {"name": "xiaoming",  "age" : 4}];var boy= {"name":"xiaoming", "brothers":["xiaohuang","xiaohong"]};

 

Note that we can define

 

VaR A = [1, 2, 3]; // defines an array alert (. length); // The result is 3A ["name"] = "Xiaoming"; // we assign alert (. length); // The result is still 3A. push (4); alert (. length); // The result is 4

 

The above code A ["name"] = "Xiaoming" adds an attribute name to array A, instead of adding data to the array.

 

In fact, we can think of arrays in this format, that is, Special Objects (the type attribute is written at will ,)

 

VaR array = {"type": "array", "value": [1, 2, 3], "length": 3 }; after a ["name"] = "Xiaoming"; is added, the array becomes like this and does not affect the value of the array. Array = {"type": "array", "name": "Xiaoming", "value": [1, 2, 3], "length": 3 };

 

 

A simple example of JavaScript Public Member definition, Private member definition, and privileged method definition is provided.

<SCRIPT> // define a javascript class function jsclass (privateparam/**/, publicparam) {// constructor var primember = privateparam; // Private variable this. pubmember = publicparam; // public variable // defines the private method function primethod () {return "primethod ()";} // define the privileged method // the privileged method can access all Members this. privilegedmethod = function () {var STR = "this is a privileged method. I have called \ n"; STR + = "private variable:" + primember + "\ n "; STR + = "Private method:" + primethod () + "\ n"; STR + = "Public variable:" + this. pubmember + "\ n"; STR + = "public method:" + this. pubmethod (); Return STR ;}/// add public methods // Private variables and Methods jsclass cannot be called. prototype. pubmethod = function () {return "pubmethod ()" ;}// jsobject = new jsclass ("primember", "pubmember"); // alert (jsobject. pubmember); // pop up the pubmember information // alert (jsobject. primember); // The undefined information is displayed // alert (jsobject. pubmethod (); // pop-up pubmethod information // alert (jsobject. primethod (); // The Error alert (jsobject. privilegedmethod (); </SCRIPT>

 

 

 

 

 

 

 

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.