当前位置:网站首页>JSON basic syntax

JSON basic syntax

2022-06-26 01:27:00 Life needs depth

JSON Grammar is JavaScript A subset of grammar .


JSON Rule of grammar

JSON Grammar is JavaScript Object represents a subset of Syntax .

  • The data is in the name / Value alignment
  • Data is separated by commas
  • Curly braces  {}  Save the object
  • brackets  []  Save array , An array can contain multiple objects

JSON name / It's worth it

JSON The data is written in :

key : value

name / Value pairs include field names ( In double quotes ), Write a colon at the back , Then it's worth :

"name" : " Novice tutorial "

It's easy to understand , Equivalent to this JavaScript sentence :

name = " Novice tutorial "


JSON value

JSON Values can be :

  • Numbers ( Integer or floating point number )
  • character string ( In double quotes )
  • Boolean value (true or false)
  • Array ( In brackets )
  • object ( In braces )
  • null

JSON Numbers

JSON Numbers can be integer or floating point :

{ "age":30 }


JSON object

JSON Objects in braces  {}  In the writing :

{key1 : value1, key2 : value2, ... keyN : valueN }

Object can contain multiple names / It's worth it :

{ "name":" Novice tutorial " , "url":"www.runoob.com" }

It's easy to understand , With this JavaScript Statement equivalence :

name = " Novice tutorial " url = "www.runoob.com"


JSON Array

JSON Array in brackets  []  In the writing :

An array can contain multiple objects :

[
    { key1 : value1-1 , key2:value1-2 }, 
    { key1 : value2-1 , key2:value2-2 }, 
    { key1 : value3-1 , key2:value3-2 }, 
    ...
    { key1 : valueN-1 , key2:valueN-2 }, 
]

{ "sites": [ { "name":" Novice tutorial " , "url":"www.runoob.com" }, { "name":"google" , "url":"www.google.com" }, { "name":" Microblogging " , "url":"www.weibo.com" } ] }

In the example above , object  sites  Is an array of three objects . Each object represents a website (name、url) The record of .


JSON Boolean value

JSON The Boolean value can be true perhaps false:

{ "flag":true }


JSON null

JSON You can set null value :

{ "runoob":null }


JSON Use JavaScript grammar

because JSON Use JavaScript grammar , So no additional software is needed to handle JavaScript Medium JSON.

adopt JavaScript, You can create an array of objects , And assign values like this :

example

var sites = [ { "name":"runoob" , "url":"www.runoob.com" }, { "name":"google" , "url":"www.google.com" }, { "name":" Microblogging " , "url":"www.weibo.com" } ];

You can visit... Like this JavaScript The first item in an array of objects ( Index from 0 Start ):

sites[0].name;

The content returned is :

runoob

You can modify the data like this :

sites[0].name=" Novice tutorial ";


Try it »

 

In the next chapter , You will learn how to put JSON The text is converted to JavaScript object .


JSON file

  • JSON The file type of the file is  .json
  • JSON Textual MIME The type is  application/json
原网站

版权声明
本文为[Life needs depth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252349340234.html