博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不可不知的json库
阅读量:4005 次
发布时间:2019-05-24

本文共 2833 字,大约阅读时间需要 9 分钟。

  • 什麼是 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,分別是  跟。

如何建立 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 的原因之一

转载地址:http://dagyi.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
python jieba分词模块的基本用法
查看>>
[CCF BY C++]2017.12 最小差值
查看>>
[CCF BY C++]2017-12 游戏
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>