Json是一種輕量級的數據交換格式,常用于前后端數據傳輸。Java自帶了許多操作Json數據的類,其中最常用的是JSONObject和JSONArray。
public class JSONObject { public JSONObject(); public JSONObject(Map copyFrom); public JSONObject(JSONTokener readFrom); public JSONObject accumulate(String name, Object value); public JSONObject append(String name, Object value) throws JSONException; public Object get(String name); public boolean getBoolean(String name) throws JSONException; public double getDouble(String name) throws JSONException; public int getInt(String name) throws JSONException; public JSONArray getJSONArray(String name) throws JSONException; public JSONObject getJSONObject(String name) throws JSONException; public long getLong(String name) throws JSONException; public String getString(String name) throws JSONException; public boolean has(String name); public boolean isNull(String name); public Iterator keys(); public int length(); public JSONArray names() throws JSONException; public JSONObject put(String name, boolean value) throws JSONException; public JSONObject put(String name, double value) throws JSONException; public JSONObject put(String name, int value) throws JSONException; public JSONObject put(String name, long value) throws JSONException; public JSONObject put(String name, Object value) throws JSONException; public JSONObject putOnce(String name, Object value) throws JSONException; public JSONObject putOpt(String name, Object value) throws JSONException; public Object remove(String name); public JSONArray toJSONArray(JSONArray names) throws JSONException; public String toString(); public String toString(int indentFactor) throws JSONException; public Writer write(Writer writer) throws JSONException; } public class JSONArray { public JSONArray(); public JSONArray(JSONTokener readFrom); public JSONArray(String json) throws JSONException; public JSONArray(Collection copyFrom); public JSONArray accumulate(Object value); public Object get(int index); public boolean getBoolean(int index) throws JSONException; public double getDouble(int index) throws JSONException; public int getInt(int index) throws JSONException; public JSONArray getJSONArray(int index) throws JSONException; public JSONObject getJSONObject(int index) throws JSONException; public long getLong(int index) throws JSONException; public String getString(int index) throws JSONException; public boolean isNull(int index); public Iterator iterator(); public int length(); public JSONArray put(boolean value) throws JSONException; public JSONArray put(double value) throws JSONException; public JSONArray put(int value) throws JSONException; public JSONArray put(long value) throws JSONException; public JSONArray put(Object value) throws JSONException; public JSONArray put(int index, boolean value) throws JSONException; public JSONArray put(int index, double value) throws JSONException; public JSONArray put(int index, int value) throws JSONException; public JSONArray put(int index, long value) throws JSONException; public JSONArray put(int index, Object value) throws JSONException; public Object remove(int index); public JSONObject toJSONObject(JSONArray names) throws JSONException; public String toString(); public String toString(int indentFactor) throws JSONException; public Writer write(Writer writer) throws JSONException; }
以上是JSONObject和JSONArray類的常用方法,使用時需要注意參數類型和異常處理。