日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSON入门之二:org.json的基本用法

發布時間:2024/1/23 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSON入门之二:org.json的基本用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


java中用于解釋json的主流工具有org.json、json-lib與gson,本文介紹org.json的應用。

官方文檔:

http://www.json.org/java/

http://developer.android.com/reference/org/json/package-summary.html

?

1、主要類

Classes


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.?

Exceptions


JSONException

Thrown to indicate a problem with the JSON API.?

?

2、構建一個JSON文本的方法

(1)使用JSONObject的構造方法,然后toString。

創建一個JSONObject對象后,再使用put(String, Object)方法添加鍵值對。

toString()方法將JSONObject對象按照JSON的標準格式進行封裝。

(2)使用JSONStringer創建JSON文本。

String myString = new JSONStringer().object() .key("name") .value("小豬") .endObject() .toString();完整程序如下:

package com.ljh.jsondemo;import org.json.JSONException; import org.json.JSONObject; import org.json.JSONStringer; import org.junit.Test;public class JSONObjectTest {@Testpublic void test() {System.out.println(prepareJSONObject());System.out.println(prepareJSONObject2());}private static String prepareJSONObject(){JSONObject studentJSONObject = new JSONObject();try {studentJSONObject.put("name", "Jason");studentJSONObject.put("id", 20130001);studentJSONObject.put("phone", "13579246810");} catch (JSONException e) {e.printStackTrace();}return studentJSONObject.toString();}private static String prepareJSONObject2(){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();}} 輸出結果如下:

{"id":20130001,"phone":"13579246810","name":"Jason"}
{"name":"Jason","id":20130001,"phone":"13579246810"}

結論:

(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.
  • 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.


3、讀取JSON文本內容。

使用JSONTokener類的相關方法。

package com.ljh.jsondemo;import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import org.junit.Test;public class JSONTokenerTEst {@Testpublic void test() {System.out.println(getJSONContent());}private static String JSONText = "{\"id\":20130001,\"phone\":\"13579246810\",\"name\":\"Jason\"}";private static String getJSONContent(){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;} } 輸出結果如下:

Jason ?20130001 ? 13579246810

事實上,使用JSONObject的構造方法也可直接創建JSONObject對象。

private static String getJSONContent2(){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; }

總結:單純使用JSONObject可以解決大部分的JSON處理問題。




總結

以上是生活随笔為你收集整理的JSON入门之二:org.json的基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。