1、什麼是JSON?
JSON是一種取代XML的數據結構,和xml相比,它更小巧但描述能力卻不差,因爲它的小巧因此網絡傳輸數據將減小更多流量從而加快速度。html
JSON就是一串字符串 只不過元素會使用特定的符號標註。java
{} 雙括號表示對象android
[] 中括號表示數組json
"" 雙引號內是屬性或值數組
: 冒號表示後者是前者的值(這個值能夠是字符串、數字、也能夠是另外一個數組或對象)網絡
因此 {"name": "Michael"} 能夠理解爲是一個包含name爲Michael的對象數據結構
而[{"name": "Michael"},{"name": "Jerry"}]就表示包含兩個對象的數組dom
固然了,你也能夠使用{"name":["Michael","Jerry"]}來簡化上面一部,這是一個擁有一個name數組的對象google
2、JSON解析之傳統的JSON解析
一、生成json字符串
public static String createJsonString(String key, Object value) { JSONObject jsonObject = new JSONObject(); jsonObject.put(key, value); return jsonObject.toString(); }
二、解析JSON字符串
分爲如下三種狀況,一個JavaBean,一個List數組,一個嵌套Map的List數組:url
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import com.android.myjson.domain.Person; /** * 完成對json數據的解析 * */ public class JsonTools { public static Person getPerson(String key, String jsonString) { Person person = new Person(); try { JSONObject jsonObject = new JSONObject(jsonString); JSONObject personObject = jsonObject.getJSONObject("person"); person.setId(personObject.getInt("id")); person.setName(personObject.getString("name")); person.setAddress(personObject.getString("address")); } catch (Exception e) { // TODO: handle exception } return person; } public static List getPersons(String key, String jsonString) { List list = new ArrayList(); try { JSONObject jsonObject = new JSONObject(jsonString); // 返回json的數組 JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject2 = jsonArray.getJSONObject(i); Person person = new Person(); person.setId(jsonObject2.getInt("id")); person.setName(jsonObject2.getString("name")); person.setAddress(jsonObject2.getString("address")); list.add(person); } } catch (Exception e) { // TODO: handle exception } return list; } public static List getList(String key, String jsonString) { List list = new ArrayList(); try { JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { String msg = jsonArray.getString(i); list.add(msg); } } catch (Exception e) { // TODO: handle exception } return list; } public static List> listKeyMaps(String key, String jsonString) { List> list = new ArrayList>(); try { JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject2 = jsonArray.getJSONObject(i); Map map = new HashMap(); Iterator iterator = jsonObject2.keys(); while (iterator.hasNext()) { String json_key = iterator.next(); Object json_value = jsonObject2.get(json_key); if (json_value == null) { json_value = ""; } map.put(json_key, json_value); } list.add(map); } } catch (Exception e) { // TODO: handle exception } return list; } }
3、JSON解析之GSON
一、生成JSON字符串
import com.google.gson.Gson; public class JsonUtils { public static String createJsonObject(Object obj) { Gson gson = new Gson(); String str = gson.toJson(obj); return str; } } 2、解析JSON import java.util.ArrayList; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; ; public class GsonTools { public GsonTools() { // TODO Auto-generated constructor stub } /** * @param * @param jsonString * @param cls * @return */ public static T getPerson(String jsonString, Class cls) { T t = null; try { Gson gson = new Gson(); t = gson.fromJson(jsonString, cls); } catch (Exception e) { // TODO: handle exception } return t; } /** * 使用Gson進行解析 List * * @param * @param jsonString * @param cls * @return */ public static List getPersons(String jsonString, Class cls) { List list = new ArrayList(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken>() { }.getType()); } catch (Exception e) { } return list; } /** * @param jsonString * @return */ public static List getList(String jsonString) { List list = new ArrayList(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken>() { }.getType()); } catch (Exception e) { // TODO: handle exception } return list; } public static List> listKeyMaps(String jsonString) { List> list = new ArrayList>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken>>() { }.getType()); } catch (Exception e) { // TODO: handle exception } return list; } }
3、JSON解析之FastJSON
import java.util.ArrayList; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; public class JsonTool { public static T getPerson(String jsonstring, Class cls) { T t = null; try { t = JSON.parseObject(jsonstring, cls); } catch (Exception e) { // TODO: handle exception } return t; } public static List getPersonList(String jsonstring, Class cls) { List list = new ArrayList(); try { list = JSON.parseArray(jsonstring, cls); } catch (Exception e) { // TODO: handle exception } return list; } public static List> getPersonListMap1( String jsonstring) { List> list = new ArrayList>(); try { list = JSON.parseObject(jsonstring, new TypeReference>>() { }.getType()); } catch (Exception e) { // TODO: handle exception } return list; } }