2.5 JSON
2.5.1.JSON格式的用途
- 在開發中
凡是涉及到『跨平臺數據傳輸』,JSON格式一定是首選
2.5.2.JSON格式的說明
- 1.JSON數據兩端
要么是{},要么是[]
- 2.JSON對象的格式是:
json {key:value,key:value,...,key:value}
- 3.JOSN數組的格式是:
[value,value,...,value]
- 4.正因為
JSON格式中value部分還可以繼續使用JSON對象或JSON數組
,所以JSON格式是可以『多層嵌套,所以JSON格式不論多么復雜的數據類型都可以表達。
var person1 = {"name":"張三瘋","age":189,"address":"武當山"
}
var person2 = {"name":"張三瘋","age":189,"address":"武當山","wife":{"name":"小花","age":18,"address":"武當山下的小村莊"}
}
var person3 = {"name":"張三瘋","age":189,"address":"武當山","wife":{"name":"小花","age":18,"address":"武當山下的小村莊"},"sons":[{"name":"奧巴馬","age":1,"address":"武當山"},{"name":"奧拉夫","age":2,"address":"少林寺"}]
}
- 5.JSON對象和JSON字符串互轉
- JSON對象轉JSON字符串
var jsonObj = {"stuName":"tom","stuAge":20};
var jsonStr = JSON.stringify(jsonObj);
console.log(typeof jsonObj);
console.log(typeof jsonStr);
jsonObj = JSON.parse(jsonStr);
console.log(jsonObj);