[02] json syntax, 02 json

Source: Internet
Author: User

[02] json syntax, 02 json

[02]

JSON syntax is a subset of JavaScript syntax.

JSON syntax rules

JSON syntax is a subset of the syntax of JavaScript Object Notation.

  • Data in name/value pairs
  • Data is separated by commas (,).
  • Brackets save objects
  • Square brackets Save the Array
JSON name/value pair

JSON data is written in the format of name/value pair.

Name/value pair includes the field name (in double quotation marks), followed by a colon, followed by a value:

  1. "firstName":"John"
 

This is easy to understand. It is equivalent to this JavaScript statement:

  1. firstName ="John"
JSON Value

The JSON value can be:

  • Number (integer or floating point number) (no quotation marks required)
  • String (in double quotation marks)
  • Logical value (true or false) (no quotation marks required)
  • Array (in square brackets)
  • Object (in curly brackets)
  • Null
JSON object

JSON objects are written in curly brackets:

The object can contain multiple name/value pairs:

  1. {"firstName":"John","lastName":"Doe"}
 

This is easy to understand and is equivalent to this JavaScript statement:

  1. firstName ="John" lastName ="Doe"
JSON Array

JSON array is written in square brackets:

An array can contain multiple objects:

  1. {
  2. "employees":[
  3. {"firstName":"John","lastName":"Doe"},
  4. {"firstName":"Anna","lastName":"Smith"},
  5. {"firstName":"Peter","lastName":"Jones"}
  6. ]
  7. }
 

In the preceding example, the object "employees" is an array containing three objects. Each object represents a record about someone (with a surname and a name.

JSON uses JavaScript syntax because JSON uses JavaScript syntax, JSON in JavaScript can be processed without additional software.
Using JavaScript, you can create an array of objects and assign values as follows:
  1. var employees =[
  2. {"firstName":"Bill","lastName":"Gates"},
  3. {"firstName":"George","lastName":"Bush"},
  4. {"firstName":"Thomas","lastName":"Carter"}
  5. ];
 

You can access the first entry in the JavaScript Object array as follows:

  1. employees[0].lastName;
 

The returned content is:

  1. Gates
 

You can modify the data as follows:

  1. employees[0].lastName ="Jobs";
 

 

JSON File
  • The JSON file type is ". json"
  • The MIME type of JSON text is "application/json"



 



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.