Json and js arrays are really not as simple as you think.
Previously, because there were few front-end tasks, the understanding of json and js arrays was limited to a fixed format. This fixed thinking at the front-end development, especially when I recently used the highcharts plug-in, made me feel particularly inclear, and finally I was puzzled by the query.
Okay, let's talk about it.
1. json
------
What is json?
First, let me see how w3c defines it.
JSON: JavaScript Object Notation (JavaScript Object Notation ). JSON is the syntax for storing and exchanging text information. Similar to XML. JSON is smaller, faster, and easier to parse than XML.
The above is a brief introduction to json. It is a javaScript Object Notation, that is, it is a js object.
JavaScript objects: All things in JavaScript are objects: strings, values, arrays, functions... In addition, javaScript allows custom objects.
The custom object format is as follows:
1) directly build an Instance object)
Person = new Object ();
Person. firstname = Bill;
Person. lastname = Gates;
Person. age = 56;
Person. eyecolor = blue;
2) person = {firstname: John, lastname: Doe, age: 50, eyecolor: blue}; this is the most common method. We recommend that you use this method for simplicity and clarity.
3) use the constructor
Function person (firstname, lastname, age, eyecolor ){
This. firstname = firstname;
This. lastname = lastname;
This. age = age;
This. eyecolor = eyecolor ;}
MyFather = new person (Bill, Gates, 56, blue );
Json format:
Person = {firstname: Bill, lastname: Gates, age: 56} we will find that the definition of a json object is different from that of a js object because the key of the js object is not used, json objects are required. Note that only double quotation marks are allowed for json objects. The test can be verified using bejson.
** A json array is actually a js array **
2. js Array
--
I will not explain the js array here, but I will focus on the js array definition method. Js defines variable var arr = (); var arr = []; var arr = new Array (); // I thought it was only possible to define these variables in this way.