org.json使用指南
org.json使用指南
@(JAVA)[json]
常見的json解釋庫有org.json, gson, json-lib, json-simple等,這里介紹了org.json的用法。
(一)概述
org.json庫非常簡單,只有4個類一個異常。單純使用JSONObject可以解決大部分的JSON處理問題。
JSONArray A dense indexed sequence of values. JSONObject A modifiable set of name/value mappings. JSONStringer Implements toString() and toString(). JSONTokener Parses a JSON (RFC 4627) encoded string into the corresponding object. JSONException Thrown to indicate a problem with the JSON API.(二)生成Json文本
1、使用JSONObject
public static String generateJsonString(){JSONObject studentJSONObject = new JSONObject();try {studentJSONObject.put("id",5);studentJSONObject.put("name", "Jason");studentJSONObject.put("phone","13579246810");} catch (JSONException e) {e.printStackTrace();}return studentJSONObject.toString();}2、使用JSONString
private static String generateJsonString2(){ JSONStringer jsonStringer = new JSONStringer(); try { jsonStringer.object(); jsonStringer.key("name"); jsonStringer.value("Jason"); jsonStringer.key("id"); jsonStringer.value(20130001); jsonStringer.key("phone"); jsonStringer.value("13579246810"); jsonStringer.endObject(); } catch (JSONException e) { e.printStackTrace(); } return jsonStringer.toString(); }3、小結(jié)
(1)使用JSONObject構(gòu)造的JSON文本順序雜亂,使用JSONStringer則按順序排列。
(2)關(guān)于二者之間的比較,android官方文檔認(rèn)為:
Most application developers should use those methods directly and disregard this API. For example:
JSONObject object = ...String json = object.toString();Stringers only encode well-formed JSON strings. In particular:
The stringer must have exactly one top-level array or object.
Lexical scopes must be balanced: every call to array() must have a matching call to endArray() and every call to object() must have a matching call to endObject().
Arrays may not contain keys (property names).
Objects must alternate keys (property names) and values.
Values are inserted with either literal value calls, or by nesting arrays or objects.
Calls that would result in a malformed JSON string will fail with a JSONException.
This class provides no facility for pretty-printing (ie. indenting) output. To encode indented output, use toString(int) or toString(int).
Some implementations of the API support at most 20 levels of nesting. Attempts to create more than 20 levels of nesting may fail with a JSONException.
Each stringer may be used to encode a single top level value. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, “Design and Document or inheritance or else prohibit it” for further information.
即:
一般情況下使用JSONObject即可,但對于一些嵌套的JSON,某些JSONArray沒有key,只有value等特殊情況,則使用JSONStringer.
(三)讀取Json文本
1、使用JSONObject
public static String getJSONContent(){ String name = null; int id = 0; String phone = null; try { JSONObject studentJSONObject = new JSONObject(JSONText); name = studentJSONObject.getString("name"); id = studentJSONObject.getInt("id"); phone = studentJSONObject.getString("phone"); } catch (JSONException e) { e.printStackTrace(); } return name + " " + id + " " + phone; }2、使用JSONTokener
public static String getJSONContent2(){ JSONTokener jsonTokener = new JSONTokener(JSONText); JSONObject studentJSONObject; String name = null; int id = 0; String phone = null; try { studentJSONObject = (JSONObject) jsonTokener.nextValue(); name = studentJSONObject.getString("name"); id = studentJSONObject.getInt("id"); phone = studentJSONObject.getString("phone"); } catch (JSONException e) { e.printStackTrace(); } return name + " " + id + " " + phone; }總結(jié)
以上是生活随笔為你收集整理的org.json使用指南的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Object.hashCode
- 下一篇: Java静态域与静态方法