Data format JSON syntax

Let's look at the definition of the json w3cschool:

JSON: JavaScript Object Notation (JavaScript Object Notation).

JSON syntax for storing and exchanging text messages. Similar to XML.

JSON is smaller than XML, faster, and easier to resolve.

JSON syntax rules

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

  • Data in the name / value pairs
  • Data separated by commas
  • Save Object braces
  • Save array square brackets

JSON name / value pairs

JSON data writing format is: name / value pairs.

Name / value pair comprises a field name (in double quotes), write behind a colon, then the value:

"firstName" : "John"

It is easy to understand, this is equivalent to the JavaScript statement:

firstName = "John"

JSON value

JSON values ​​can be:

  • Number (integer or floating point)
  • String (in double quotes)
  • A logical value (true or false)
  • An array (in square brackets)
  • Objects (in braces)
  • null

JSON object

JSON object written in curly braces:

Object may comprise a plurality of name / value pairs:

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

It is also easy to understand, and this statement is equivalent JavaScript:

firstName = "John"
lastName = "Doe"

JSON array

JSON array written in square brackets:

Array may comprise a plurality of objects:

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

In the above example, the object "employees" is an array comprising three objects. Each object represents a record of a person (name and surname) of.

JSON using JavaScript syntax

Because JSON using JavaScript syntax, so no additional software will be able to handle JavaScript in JSON.

By JavaScript, you can create an array of objects, and assignment like this:

example

var employees = [
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName": "Carter" }
];

The first item like this can access an array of JavaScript objects:

employees[0].lastName;

Content is returned:

Gates

You can modify the data like this:

employees[0].lastName = "Jobs";

Guess you like

Origin www.cnblogs.com/dingjiaoyang/p/10986297.html