Json and js arrays are really not as simple as you think.

Source: Internet
Author: User
Tags array definition

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.

 

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.