org.json使用指南
org.json使用指南@(博客文章)[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、使用JSONObjectpublic 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、使用JSONStringprivate 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、小结(1)使用JSONObject构造的JSON文本顺序杂乱,使用JSONStringer则按顺序排列。 (2)关于二者之间的比较,android官方文档认为: 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. 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. 即: (三)读取Json文本1、使用JSONObjectpublic 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、使用JSONTokenerpublic 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |