摘自https://blog.csdn.net/liyuanjinglyj/article/details/45890825json
1.JSON數組
JSON是JavaScript Object Notation的縮寫,是JavaScript標準的一個子集。官方Android API已經內置支持讀寫JSON數據。這種格式很是適合表示不包含二進制數據的複雜對象。從某種程度上說,它也成了網絡上共享數據的事實標準。服務器
下面的例子顯示了一個簡單的JSON數組,它包含3個對象,每一個對象都存儲People的信息。這種格式很是適合在網絡服務上發送任務或者直接在朋友中共享數據。網絡
[函數
{this
"name":"liyuanjinglyj",google
"age":"22",.net
"lon":"12"對象
},blog
{
"name":"fengxinyao",
"age":"24",
"lon":"22"
},
{
"name":"hefan",
"age":"23",
"lon":"11"
}
]
從InputStream讀取JSON數據最好使用JsonReader API,以下所示:
public JSONArray readPeopleFromInputStream(InputStream inputStream){ InputStreamReader reader=new InputStreamReader(inputStream); JsonReader jsonReader=new JsonReader(reader); JSONArray jsonArray=new JSONArray(); try { jsonReader.beginArray(); while(jsonReader.hasNext()){ JSONObject jsonObject=readSingleJSON(jsonReader); jsonArray.put(jsonObject); } jsonReader.endArray(); } catch (Exception e) { e.printStackTrace(); } return jsonArray; } private JSONObject readSingleJSON(JsonReader jsonReader)throws Exception{ JSONObject jsonObject=new JSONObject(); jsonReader.beginObject(); JsonToken token; do{ String name=jsonReader.nextName(); if("name".equals(name)){ jsonObject.put("name",jsonReader.nextString()); }else if("age".equals(name)){ jsonObject.put("age",jsonReader.nextString()); }else if("lon".equals(name)){ jsonObject.put("lon",jsonReader.nextString()); } token=jsonReader.peek(); }while(token!=null&&!token.equals(JsonToken.END_OBJECT)); jsonReader.endObject(); return jsonObject; }
雖然也能夠把InputStream中的所有內容都讀到String中,而後傳給JSONArray的構造函數,但前面的方法消耗內存少,而且極可能很快。一樣,JsonWriter類容許OutputStream高效地寫入JSON數據,以下所示:
public void writePeopleJSON(JSONArray jsonArray,OutputStream outputStream) throws Exception { OutputStreamWriter write=new OutputStreamWriter(outputStream); JsonWriter jsonWrite=new JsonWriter(write); int arrayLength=jsonArray.length(); jsonWrite.beginArray(); for (int i = 0; i < arrayLength; i++) { JSONObject jsonObject= (JSONObject) jsonArray.get(i); jsonWrite.beginObject(); jsonWrite.name("name").value(jsonObject.getString("name")); jsonWrite.name("age").value(jsonObject.getString("age")); jsonWrite.name("lon").value(jsonObject.getString("lon")); jsonWrite.endObject(); } jsonWrite.endArray(); jsonWrite.close(); }
2.使用Gson進行高級JSON處理
JSONObject和JSONArray類使用起來很方便,但它們有必定的侷限性,而且一般會消耗更多沒必要要的內存。一樣,若是有多個不一樣類型的對象,使用JsonReader和JsonWriteer須要編寫至關多的代碼。若是爲更高級的JSON數據序列化和反序列化方法,可使用優秀的開源庫Gson。
Gson容許把簡單的Java對象(Plain Old Object,POJO)轉換成JSON,反之亦然。開發者所要作的就是把數據定義成普通的Java對象,提供get和set方法,並在項目中引入Gson庫。
下面的類顯示了一個表示任務的簡單Java對象:
public class People { private String name; private String age; private String lon; public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public void setLon(String lon) { this.lon = lon; } public String getName() { return name; } public String getAge() { return age; } public String getLon() { return lon; } }
下面的代碼顯示如何讀取和寫入Collection<People>對象集合。序列化形式始終是有效的JSON數據,所以向Web服務發佈JSON格式的數據時選擇它會很方便。若是開發者也負責服務器端的代碼,而且剛好使用Java語言,那麼就能夠在服務器端和Android應用間輕鬆的共享同一組Java代碼。
public Collection<People> readPeopleFromStream(InputStream inputStream){ InputStreamReader reader=new InputStreamReader(inputStream); Gson gson=new Gson(); Type type=new TypeToken<Collection<People>>(){}.getType(); return gson.fromJson(reader,type); } public void writePeopleToStream(Collection<People> peoples,OutputStream outputStream) throws IOException { OutputStreamWriter write=new OutputStreamWriter(outputStream,"UTF-8"); com.google.gson.stream.JsonWriter jsonWrite= new com.google.gson.stream.JsonWriter(write); Gson gson=new Gson(); Type type=new TypeToken<Collection<People>>(){}.getType(); gson.toJson(peoples,type,jsonWrite); Log.i("MainActivity", "GSON"); jsonWrite.flush(); jsonWrite.close(); }