「Json」:修訂間差異

出自TSK WIKI
跳至導覽 跳至搜尋
Root​(留言 | 貢獻)
Root​(留言 | 貢獻)
 
(未顯示同一使用者於中間所作的 17 次修訂)
第1行: 第1行:
==JSON格式說明==
==JSON格式說明==
*JavaScript Object Notation
*JavaScript Object Notation
*[http://www.jsoneditoronline.org/ JSON格式化的網站]
*物件(object)用大括號 { }
*物件(object)用大括號 { }
**key-value的方式儲存
**ex {"subject":"Math","score":80}
**json object的鍵值(key),一定要用文字做鍵值
*陣列(array)用中括號 [ ]
*陣列(array)用中括號 [ ]
*[http://www.jsoneditoronline.org/ JSON格式化的網站]
**ex [0,4,5,2,7,8,3]
<PHP>
[
{"name":"Tom",
  "lastname":"Chen",
  "report":[
  {
  "subject":"Math",
  "score":80
  },
  {"subject":"English",
  "score":90}]
  },
  {
    "name":"Amy",
    "lastname":"Lin",
    "report":
    [
    {
      "subject":"Math",
      "score":86
      },
      {
      "subject":"English",
      "score":88
      }
    ]
  }
]
</PHP>
==PHP vs JSON==
*[https://blog.wu-boy.com/2011/04/%E4%BD%A0%E4%B8%8D%E5%8F%AF%E4%B8%8D%E7%9F%A5%E7%9A%84-json-%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%B4%B9/ 你不可不知的 JSON 基本介紹]
* json_encode (將array->json 對變量進行JSON編碼)
* json_decode (接受一個JSON 格式的字符串並且把它轉換為PHP 變量)
<php>
$cart = array(
  "orderID" => 12345,
  "shopperName" => "John Smith",
  "shopperEmail" => "johnsmith@example.com",
  "contents" => array(
    array(
      "productID" => 34,
      "productName" => "SuperWidget",
      "quantity" => 1
    ),
    array(
      "productID" => 56,
      "productName" => "WonderWidget",
      "quantity" => 3
    )
  ),
  "orderCompleted" => true
);
echo json_encode( $cart );
?>
/*
{"orderID":12345,"shopperName":"John Smith",
"shopperEmail":"johnsmith@example.com","contents":
[{"productID":34,"productName":"SuperWidget","quantity":1},
{"productID":56,"productName":"WonderWidget","quantity":3}],
"orderCompleted":true}
*/
</php>
<php>
$jsonString = '
{                                         
  "orderID": 12345,                       
  "shopperName": "John Smith",             
  "shopperEmail": "johnsmith@example.com", 
  "contents": [                           
    {                                     
      "productID": 34,                     
      "productName": "SuperWidget",       
      "quantity": 1                       
    },                                     
    {                                     
      "productID": 56,                     
      "productName": "WonderWidget",       
      "quantity": 3                       
    }                                     
  ],                                       
  "orderCompleted": true                   
}                                         
';
$cart = json_decode( $jsonString );
echo $cart->shopperEmail . "<br>";
echo $cart->contents[1]->productName . "<br>";
</php>
 
==JQUERY VS JSON==
<vb>
{
  "orderID": 12345,
  "shopperName": "John Smith",
  "shopperEmail": "johnsmith@example.com",
  "contents": [
    {
      "productID": 34,
      "productName": "SuperWidget",
      "quantity": 1
    },
    {
      "productID": 56,
      "productName": "WonderWidget",
      "quantity": 3
    }
  ],
  "orderCompleted": true
}
</vb>
<vb>
<script type="text/javascript">
var cart = {
  "orderID": 12345,
  "shopperName": "John Smith",
  "shopperEmail": "johnsmith@example.com",
  "contents": [
    {
      "productID": 34,
      "productName": "SuperWidget",
      "quantity": 1
    },
    {
      "productID": 56,
      "productName": "WonderWidget",
      "quantity": 3
    }
  ],
  "orderCompleted": true
};
</script>
</vb>
*getJSON
*parseJSON
*JSON.stringify
<vb>
<script type="text/javascript">
var cart = {
  "orderID": 12345,
  "shopperName": "John Smith",
  "shopperEmail": "johnsmith@example.com",
  "contents": [
    {
      "productID": 34,
      "productName": "SuperWidget",
      "quantity": 1
    },
    {
      "productID": 56,
      "productName": "WonderWidget",
      "quantity": 3
    }
  ],
  "orderCompleted": true
};
alert ( JSON.stringify( cart ) );
</script>
/*
{"orderID":12345,"shopperName":"John Smith","shopperEmail":"johnsmith@example.com",
"contents":[{"productID":34,"productName":"SuperWidget","quantity":1},
{"productID":56,"productName":"WonderWidget","quantity":3}],
"orderCompleted":true}
*/
</vb>
 
*JSON.parse
<vb>
<script type="text/javascript">
var jsonString = '                          \
{                                          \
  "orderID": 12345,                        \
  "shopperName": "John Smith",              \
  "shopperEmail": "johnsmith@example.com",  \
  "contents": [                            \
    {                                      \
      "productID": 34,                      \
      "productName": "SuperWidget",        \
      "quantity": 1                        \
    },                                      \
    {                                      \
      "productID": 56,                      \
      "productName": "WonderWidget",        \
      "quantity": 3                        \
    }                                      \
  ],                                        \
  "orderCompleted": true                    \
}                                          \
';
var cart = JSON.parse ( jsonString );
alert ( cart.shopperEmail );
alert ( cart.contents[1].productName );
</script>
/*
結果如下
cart.shopperEmail 輸出 johnsmith@example.com cart.contents[1].productName 輸出 WonderWidget
*/
</vb>

於 2016年12月29日 (四) 14:50 的最新修訂

JSON格式說明

  • JavaScript Object Notation
  • JSON格式化的網站
  • 物件(object)用大括號 { }
    • key-value的方式儲存
    • ex {"subject":"Math","score":80}
    • json object的鍵值(key),一定要用文字做鍵值
  • 陣列(array)用中括號 [ ]
    • ex [0,4,5,2,7,8,3]

<PHP> [

{"name":"Tom",
 "lastname":"Chen",
 "report":[
 {
  "subject":"Math",
  "score":80
  },
 {"subject":"English",
  "score":90}]
  },
  {
   "name":"Amy",
   "lastname":"Lin",
   "report":
   [
    {
     "subject":"Math",
     "score":86
     },
     {
      "subject":"English",
      "score":88
      }
   ]
  }

] </PHP>

PHP vs JSON

<php> $cart = array(

 "orderID" => 12345,
 "shopperName" => "John Smith",
 "shopperEmail" => "johnsmith@example.com",
 "contents" => array(
   array(
     "productID" => 34,
     "productName" => "SuperWidget",
     "quantity" => 1
   ),
   array(
     "productID" => 56,
     "productName" => "WonderWidget",
     "quantity" => 3
   )
 ),
 "orderCompleted" => true

);

echo json_encode( $cart ); ?> /* {"orderID":12345,"shopperName":"John Smith", "shopperEmail":"johnsmith@example.com","contents": [{"productID":34,"productName":"SuperWidget","quantity":1}, {"productID":56,"productName":"WonderWidget","quantity":3}], "orderCompleted":true}

  • /

</php> <php> $jsonString = ' {

 "orderID": 12345,                         
 "shopperName": "John Smith",              
 "shopperEmail": "johnsmith@example.com",  
 "contents": [                             
   {                                       
     "productID": 34,                      
     "productName": "SuperWidget",         
     "quantity": 1                        
   },                                      
   {                                       
     "productID": 56,                      
     "productName": "WonderWidget",        
     "quantity": 3                         
   }                                       
 ],                                        
 "orderCompleted": true                    

} ';

$cart = json_decode( $jsonString ); echo $cart->shopperEmail . "
"; echo $cart->contents[1]->productName . "
"; </php>

JQUERY VS JSON

<vb> {

 "orderID": 12345,
 "shopperName": "John Smith",
 "shopperEmail": "johnsmith@example.com",
 "contents": [
   {
     "productID": 34,
     "productName": "SuperWidget",
     "quantity": 1
   },
   {
     "productID": 56,
     "productName": "WonderWidget",
     "quantity": 3
   }
 ],
 "orderCompleted": true

} </vb> <vb> <script type="text/javascript"> var cart = {

 "orderID": 12345,
 "shopperName": "John Smith",
 "shopperEmail": "johnsmith@example.com",
 "contents": [
   {
     "productID": 34,
     "productName": "SuperWidget",
     "quantity": 1
   },
   {
     "productID": 56,
     "productName": "WonderWidget",
     "quantity": 3
   }
 ],
 "orderCompleted": true

}; </script> </vb>

  • getJSON
  • parseJSON
  • JSON.stringify

<vb> <script type="text/javascript">

var cart = {

 "orderID": 12345,
 "shopperName": "John Smith",
 "shopperEmail": "johnsmith@example.com",
 "contents": [
   {
     "productID": 34,
     "productName": "SuperWidget",
     "quantity": 1
   },
   {
     "productID": 56,
     "productName": "WonderWidget",
     "quantity": 3
   }
 ],
 "orderCompleted": true

};

alert ( JSON.stringify( cart ) );

</script> /* {"orderID":12345,"shopperName":"John Smith","shopperEmail":"johnsmith@example.com", "contents":[{"productID":34,"productName":"SuperWidget","quantity":1}, {"productID":56,"productName":"WonderWidget","quantity":3}], "orderCompleted":true}

  • /

</vb>

  • JSON.parse

<vb> <script type="text/javascript">

var jsonString = ' \ { \

 "orderID": 12345,                         \
 "shopperName": "John Smith",              \
 "shopperEmail": "johnsmith@example.com",  \
 "contents": [                             \
   {                                       \
     "productID": 34,                      \
     "productName": "SuperWidget",         \
     "quantity": 1                         \
   },                                      \
   {                                       \
     "productID": 56,                      \
     "productName": "WonderWidget",        \
     "quantity": 3                         \
   }                                       \
 ],                                        \
 "orderCompleted": true                    \

} \ ';

var cart = JSON.parse ( jsonString );

alert ( cart.shopperEmail ); alert ( cart.contents[1].productName );

</script> /* 結果如下 cart.shopperEmail 輸出 johnsmith@example.com cart.contents[1].productName 輸出 WonderWidget

  • /

</vb>