<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"></meta>
<title>JSON 教學</title>
<script type="text/javascript">
// JSON: javascript object notation(javascript 物件表示法)
//js new一個物件
/* var point = new Object();
point x=3;
point y=4;
point.get=function(){
alert(this.x + "," this.y);
}
*/
//JSON 建立物件的格式(json 的基本動作)
var point = {
"x":3,
"y":4,
"comeon":function(){
alert(this.x + "," + this.y)
}
}
//point.comeon();
/*
物件結構與 JSON 格式字串轉換,使用内建 JSON 物件中的方法
使用 JSON.stringify(物件);將物件轉換成 JSON 格式的字串,但會忽略函式的部分
使用 JSON.parse(JSON 格式字串;) 將字串轉換成物件結構
*/
// 用JSON 物件轉換字串
var jsonStr=JSON.stringify(point);
console.log(jsonStr);
// 用JSON 字串轉換物件
var plainObj=JSON.parse(jsonStr);
console.log(plainObj);
alert(plainObj);
</script>
</head>
<!-- 網頁初始化 首頁的顯示 -->
<body>
</body>
</html>