json使用類型總結


一、json值:

1、JSON 值可以是: 

(1)數字(整數或浮點數)

(2)字符串(在雙引號中)

(3)邏輯值(true false

(4)數組(在中括號中)

(5)對象(在大括號中)

(6)null

2、舉例:

(1)json數字:JSON 數字可以是整型或者浮點型:

{ "age":30 }

(2)json對象:JSON 對象在大括號({})中書寫,對象可以包含多個名稱/值對:

{ "name":"菜鳥教程" , "url":"www.runoob.com" }

(3)json數組:JSON 數組在中括號中書寫,數組可包含多個對象:

{
"sites": [
{ "name":"菜鳥教程" , "url":"www.runoob.com" }, 
{ "name":"google" , "url":"www.google.com" }, 
{ "name":"微博" , "url":"www.weibo.com" }
]
}

(4)json布爾值:JSON 布爾值可以是 true 或者 false:

{ "flag":true }

(5)json null:

{ "runoob":null }

二、json使用javascript語法:

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

三、json對象:

1、json對象語法:

myObj = { "name":"runoob", "alexa":10000, "site":null };

2、嵌套json對象:  

myObj = {
    "name":"runoob",
    "alexa":10000,
    "sites": {
        "site1":"www.runoob.com",
        "site2":"m.runoob.com",
        "site3":"c.runoob.com"
    }
}

四、json數組:

1、json數組語法:JSON 數組在中括號中書寫

[ "Google", "Runoob", "Taobao" ]

2、json對象中的數組:

{
"name":"網站",
"num":3,
"sites":[ "Google", "Runoob", "Taobao" ]
}

3、嵌套json對象中的數組:JSON 對象中數組可以包含另外一個數組,或者另外一個 JSON 對象

myObj = {
    "name":"網站",
    "num":3,
    "sites": [
        { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻譯" ] },
        { "name":"Runoob", "info":[ "菜鳥教程", "菜鳥工具", "菜鳥微信" ] },
        { "name":"Taobao", "info":[ "淘寶", "網購" ] }
    ]
}

參考來源:菜鳥教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM