0
0

More than 5 years have passed since last update.

JSON 基本教學 By 彭彭 24

Last updated at Posted at 2018-08-19
9. JSON 基本教學
<!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>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0