讀取網頁字串 (1)
description
Transcript of 讀取網頁字串 (1)
讀取網頁字串 (1)
讀取網頁字串 (2)
讀取網頁字串 (3)
讀取網頁字串 (4)
什麼是 JSON
• JSON ( JavaScript Object Notation )是一種羽量級的資料交換格式,易於閱讀和編寫,同時也易於機器解析和生成,非常適合於伺服器與用戶端的交互。 JSON 採用與程式設計語言無關的文本格式,但是也使用了類 C語言的習慣,這些特性使 JSON 成為理想的資料交換格式。
• 和 XML 一樣, JSON 也是基於純文字的資料格式。 JSON 的資料格式非常簡單,您可以用 JSON 傳輸一個簡單的 String , Number , Boolean ,也可以傳輸一個陣列,或者一個複雜的 Object 物件。
JSON 的優點
• 相容性高• 格式容易瞭解,閱讀及修改方便• 支援許多資料格式
(number,string,booleans,nulls,array,associative array)
• 許多程式都支援函式庫讀取或修改 JSON 資料
number
string
value
object
false
null
array
true
JSON 的語法
• A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
{ : }valuestring
object
,
JSON 的語法
• An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by, (comma).
{ "name": "Jack B. Nimble", "at large": true, "grade": "A", "format": { "type": "rect", "width": 1920, "height": 1080, "interlace": false, "framerate": 24 }}
object 範例
[ ]value
array
,
JSON 的語法
• An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
[[0, -1, 0],
[1, 0, 0],
[0, 0, 1]
]
array 範例
JSON 與 XML 對應
JSON
XML
一個簡單的 JSON 範例JSON字串 :
JSON demo
JSONArray items = new JSONArray(body);
for (int i = 0;i<items.length();i++){ JSONObject json_data = items.getJSONObject(i);
String data1 = json_data.getString("鍵值名稱 "); int data2 = json_data.getInt("鍵值名稱 "); double data3 = json_data.getDouble("鍵值名稱 ");}
Java 讀取 JSON