不可不知的json库

  • 什麼是 JSON
  • JSON 應用在哪些地方
  • 如何建立 JSON 字串
  • 一個簡單的 JSON 範例
  • JSON vs XML
  • 如何透過 PHP 及 JavaScript 使用 JSON

什麼是 JSON

JSON 是個以純文字為基底去儲存和傳送簡單結構資料,你可以透過特定的格式去儲存任何資料(字串,數字,陣列,物件),也可以透過物件或陣列來傳送較複雜的資料。一旦建立了您的 JSON 資料,就可以非常簡單的跟其他程式溝通或交換資料,因為 JSON 就只是純文字個格式。

JSON 的優點如下:

  • 相容性高
  • 格式容易瞭解,閱讀及修改方便
  • 支援許多資料格式 (number,string,booleans,nulls,array,associative array)
  • 許多程式都支援函式庫讀取或修改 JSON 資料

JSON 應用在哪些地方

JSON 最常用用在 Web 網頁程式從 Server 端傳送資料給 browser,典型範例就是透過 AJAX 方式交換 JSON 資料,底下簡單舉個範例

1. 使用者點選了線上產品縮圖
2. JavaScript 透過 AJAX 方式將產品 ID 傳送給伺服器端
3. 伺服器端收到 ID,將產品資料 (ex 價格,描述) 編碼成 JSON 資料,並且回傳給瀏覽器
4. JavaScript 收到 JSON 資料,將其解碼 (decode) 並且將資料顯示在網頁上

您也可以透過網頁將 JSON 資料傳到伺服器端,這都是可以的,把 POST 或 GET 資訊編碼成 JSON 格式即可,如果有在使用 jQuery,它提供了兩個函式處理 JSON,分別是 getJSON 跟parseJSON

如何建立 JSON 字串

可以透過底下規則來建立 JSON 字串

1. JSON 字串可以包含陣列 Array 資料或者是物件 Object 資料
2. 陣列可以用 [ ] 來寫入資料
3. 物件可以用 { } 來寫入資料
4. name / value 是成對的,中間透過 (:) 來區隔

物件或陣列的 value 值可以如下:

1. 數字 (整數或浮點數)
2. 字串 (請用 “” 括號)
3. 布林函數 (boolean) (true 或 false)
4. 陣列 (請用 [ ] )
5. 物件 (請用 { } )
6. NULL

一個簡單的 JSON 範例

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

由上面例子我們可以發現 contents 陣列裡面又包含物件,透過上面例子,我們寫成 JavaScript 如下:

<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 >

JSON vs XML

在許多方面,你可以想像 JSON 來替代 XML,在過去 Web Application 開發 AJAX 都是透過 XML 來交換資料,但是你可以發現近幾年來 JSON 已經漸漸取代 XML 格式了,為什麼會變成這樣呢?因為 JSON 格式容易閱讀且好修改,許多程式語言分別開發了函式庫來處理 JSON 資料,我們可以把上面的 JSON 資料改寫成 XML 如下:

<object >
   <property >
     <key >orderID </key >
     <number > 12345 </number >
   </property >
   <property >
     <key >shopperName </key >
     <string >John Smith </string >
   </property >
   <property >
     <key >shopperEmail </key >
     <string >johnsmith @example. com </string >
   </property >
   <property >
     <key >contents </key >
     <array >
       <object >
         <property >
           <key >productID </key >
           <number > 34 </number >
         </property >
         <property >
           <key >productName </key >
           <string >SuperWidget </string >
         </property >
         <property >
           <key >quantity </key >
           <number > 1 </number >
         </property >        
       </object >
       <object >
         <property >
           <key >productID </key >
           <number > 56 </number >
         </property >
         <property >
           <key >productName </key >
           <string >WonderWidget </string >
         </property >
         <property >
           <key >quantity </key >
           <number > 3 </number >
         </property > 
       </object >
     </array >
   </property >
   <property >
     <key >orderCompleted </key >
     <boolean >true </boolean >
   </property >  
</object >

大家有沒有發現 XML 的資料量遠大於 JSON 資料量,這也是 JSON 優於 XML 的原因之一

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值