当前位置:网站首页>JSON introduction

JSON introduction

2022-06-11 20:33:00 be raining

1. json Introduction to

json yes JavaScript Object Notation An acronym for , Which translates as javascript Object notation , here json Namely Be similar to javascript Object's string , It is also a kind of data format , At present, this data format is quite popular , Gradually replaced the traditional xml data format .

2. json The format of

json There are two formats :

  1. Object format
  2. The array format

Object format :

Object format json data , Use a pair of braces ({}), Put... In braces key:value Key value pairs of form , Multiple key value pairs are separated by commas .

Object format json data :

{
    "name":"tom",
    "age":18
}

Format specification :

json Medium (key) The attribute name and string value need to be written in Double quotes Lead up , Using single quotation marks or not using quotation marks will result in reading data errors .

The array format :

In array format json data , Use a pair of brackets ([]), The data in brackets is separated by commas .

In array format json data :

["tom",18,"programmer"]

Actually developed json The format is more complex , for example :

{
    "name":"jack",
    "age":29,
    "hobby":["reading","travel","photography"]
    "school":{
        "name":"Merrimack College",
        "location":"North Andover, MA"
    }
}

3. json Data to JavaScript object

json It's essentially a string , If in js In the operation json data , Can be json String to JavaScript object .

Sample code :

var sJson = '{"name":"tom","age":18}';
var oPerson = JSON.parse(sJson);

//  Operation properties 
alert(oPerson.name);
alert(oPerson.age);

4. Summary

  • json It's just one. javascript Object notation ,json Essentially a string .
  • json There are two formats :1. Object format , 2. The array format
原网站

版权声明
本文为[be raining]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011746472153.html