JSON 解析 – java庫org.json簡介

來源:互聯網
上載者:User

鍵類型:

必須是 字串

實值型別:
可以是 字串 數字 boolean 對象 數組 空值
e.g. "hello"
e.g. 100.0
e.g. true
e.g. {"a":1, "b":"val_string", "c":true}
e.g. ["a", "b", "c", 100.0, true]
e.g. null

對象:
大括弧包圍的 索引值對的無序組合
{"a":1, "b":true, "c":"c_str", "prev_a":{"a":1, "b":2, "c":3}, "d":100.0}

數組:
中括弧包圍的 實值型別的順序組合
["a", "b", "c", true, 100.0, null, {"a":1, "b":2, "c":3}, [1, 2, 3, "a", "b", "c"]]

參考文獻:
http://www.json.org/

java版本的json解析包(來自json.org網站的原始碼),(另存新檔, 再重新命名為json-parser-v1.0.jar)

http://hi.csdn.net/attachment/201107/19/0_13110714569ee8.gif


主要類:

1) JSONObject 常用方法:

- names(), 擷取鍵的集合;

- optString(String args), 擷取鍵args指定的 字串類型 的值;

- optInt(String args), 擷取 整型的 值;

- optJSONObject(String args), 擷取JSONObject, 遞迴解析;

- optJSONArray(String args), 擷取JSONArray數組,遍曆;


2) JSONArray 常用方法:

- length(), 數組個數

- optString(int index), 擷取索引index位置的,字串類型的值;

- optJSONObject(int index), 擷取JSONObject對象,遞迴解析;

- optJSONArray(int index), 擷取數組,遞迴遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.