转载请注明出处:http://blog.csdn.net/yegongheng/article/details/38295227
/**-----------------Gson数据解析----------------------------*/ /** * 将Json字符串解析成对应的Java对象 * @param jsonStr 需要解析的Json字符串 * @param mClass 需要解析成的Java对象类型 * @return 解析后的Java对象实例 */ public static <T> T gsonToObject(String jsonStr,Class<T> mClass) { Gson mGson = new Gson(); T mt = mGson.fromJson(jsonStr, mClass); return mt; } /** * 将Json字符串解析成对应的ArrayList<T>集合 * @param jsonStr 需要解析的Json字符串 * @param mType 需要解析成的Java对象类型 * @return mlList 解析后的ArrayList<T>集合 */ public static <T> ArrayList<T> gsonToListObjectOrString(String jsonStr,Type mType) { Gson mGson = new Gson(); ArrayList<T> mlList = mGson.fromJson(jsonStr,mType); return mlList; } /** * 将Json字符串解析成对应的ArrayList<Map<String,T>>集合 * @param jsonStr 需要解析的Json字符串 * @param mType 需要解析成的Java对象类型 * @return mapsList 解析后的ArrayList<Map<String,T>>集合 */ public static <T> ArrayList<Map<String, T>> gsonGetListMapObject(String jsonStr,Type mType) { Gson mGson = new Gson(); ArrayList<Map<String, T>> mapsList = mGson.fromJson(jsonStr, mType); return mapsList; } /** * 获取需要转换的List<T>类型 * @param mClass * @return mType */ public static <T> Type getListType(Class<T> mClass){ Type mType = null; if(mClass == Person.class){ mType = new TypeToken<List<Person>>(){}.getType(); }else if(mClass == String.class){ mType = new TypeToken<List<String>>(){}.getType(); } return mType; } /** * 获取需要转换的List<Map<String,T>>类型 * @param mClass * @return mType */ public static <T> Type getListMapType(Class<T> mClass){ Type mType = null; if(mClass == Person.class){ mType = new TypeToken<List<Map<String, Person>>>(){}.getType(); }else if(mClass == String.class){ mType = new TypeToken<List<Map<String, String>>>(){}.getType(); } return mType; }然后在GsonActivity中编写展示解析完成的Json数据,具体的代码如下:
public class GsonActivity extends BaseActivity { @Override public void onDownloaded(String result, int typeId) { // TODO Auto-generated method stub System.out.println("result = "+ result); ArrayList<String> mList = new ArrayList<String>(); switch (typeId) { case R.id.json_person: Person mPerson = JsonTools.gsonToObject(result, Person.class); mList.add(mPerson.toString()); break; case R.id.json_string: ArrayList<String> mArrayList = JsonTools.gsonToListObjectOrString(result,JsonTools.getListType(String.class)); mList.addAll(mArrayList); break; case R.id.json_list_person: ArrayList<Person> mPersons = JsonTools.gsonToListObjectOrString(result,JsonTools.getListType(Person.class)); mList.addAll(personsToString(mPersons)); break; case R.id.json_listmap_person: ArrayList<Map<String, Person>> maps = JsonTools.gsonGetListMapObject(result,JsonTools.getListMapType(Person.class)); mList.addAll(listmappersonsToString(maps)); break; default: break; } bindData(mList); } }
/**-----------------Jackson数据解析----------------------------*/ private static ObjectMapper mObjectMapper; private static JsonGenerator mJsonGenerator; private static ByteArrayOutputStream mOutputStream; public JsonTools() { mOutputStream = new ByteArrayOutputStream(); //实例化ObjectMapper对象 mObjectMapper = new ObjectMapper(); try { //实例化JsonGenerator对象 mJsonGenerator = mObjectMapper.getFactory().createGenerator(mOutputStream, JsonEncoding.UTF8); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 将Json字符串解析成对应的Java对象 * @param jsonStr 需要解析的Json字符串 * @param mClass 需要解析成的Java对象类型 * @return mt 解析后的Java对象 */ public <T> T JacksonToObject(String jsonStr,Class<T> mClass) { T mt = null; try { mt = mObjectMapper.readValue(jsonStr, mClass); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mt; } /** * 将Json字符串解析成对应的ArrayList<T>对象 * @param jsonStr 需要解析的Json字符串 * @param mTypeReference 需要解析成的Java对象类型引用 * @return 解析后的ArrayList<T>集合 */ public <T> ArrayList<T> JacksonToListObjectOrString(String jsonStr,com.fasterxml.jackson.core.type.TypeReference<T> mTypeReference) { ArrayList<T> mList = null; try { mList = mObjectMapper.readValue(jsonStr, mTypeReference); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mList; } /** * 将Json字符串解析成对应的ArrayList<Map<String,T>>对象 * @param jsonStr 需要解析的Json字符串 * @param mTypeReference 需要解析成的Java对象类型引用 * @return 解析后的ArrayList<Map<String, T>>集合 */ public <T> ArrayList<Map<String, T>> JacksonToListMapObject(String jsonStr,com.fasterxml.jackson.core.type.TypeReference<T> mTypeReference) { ArrayList<Map<String, T>> mapsList = null; try { mapsList = mObjectMapper.readValue(jsonStr, mTypeReference); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return mapsList; } /** * 获取需要转换的List<T>类型 * @param mClass * @return */ public static <T> com.fasterxml.jackson.core.type.TypeReference<T> getListTypeReference(Class<T> mClass){ com.fasterxml.jackson.core.type.TypeReference mTypeReference = null; if(mClass == Person.class){ mTypeReference = new com.fasterxml.jackson.core.type.TypeReference<List<Person>>() { }; }else if(mClass == String.class){ mTypeReference = new com.fasterxml.jackson.core.type.TypeReference<List<String>>() { }; } return mTypeReference; } /** * 获取需要转换的List<Map<String,T>>类型 * @param mClass * @return */ public static <T> com.fasterxml.jackson.core.type.TypeReference<T> getListMapTypeReference(Class<T> mClass){ com.fasterxml.jackson.core.type.TypeReference mTypeReference = null; if(mClass == Person.class){ mTypeReference = new com.fasterxml.jackson.core.type.TypeReference<List<Map<String, Person>>>() { }; }else if(mClass == String.class){ mTypeReference = new com.fasterxml.jackson.core.type.TypeReference<List<Map<String, Person>>>() { }; } return mTypeReference; }
public class JacksonActivity extends BaseActivity { @Override public void onDownloaded(String result, int typeId) { // TODO Auto-generated method stub ArrayList<String> mList = new ArrayList<String>(); JsonTools mJsonTools = new JsonTools(); switch (typeId) { case R.id.json_person: Person mPerson = mJsonTools.JacksonToObject(result, Person.class); mList.add(mPerson.toString()); break; case R.id.json_string: ArrayList<String> mArrayList = mJsonTools.JacksonToListObjectOrString(result,JsonTools.getListTypeReference(String.class)); mList.addAll(mArrayList); break; case R.id.json_list_person: ArrayList<Person> mPersons = mJsonTools.JacksonToListObjectOrString(result,JsonTools.getListTypeReference(Person.class)); mList.addAll(personsToString(mPersons)); break; case R.id.json_listmap_person: ArrayList<Map<String, Person>> maps = mJsonTools.JacksonToListMapObject(result,JsonTools.getListMapTypeReference(Person.class)); mList.addAll(listmappersonsToString(maps)); break; default: break; } bindData(mList); } }接下来便在Android模拟器上运行程序,运行效果见文章末尾。要学习更多有关Jackson解析的知识,可参考以下网站:http://jackson.codehaus.org/。
/**-----------------FastJson数据解析----------------------------*/ /** * 将Json字符串解析成对应的Java对象 * @param jsonStr 需要解析的Json字符串 * @param mClass 需要解析成的Java对象类型 * @return 解析后的Java对象实例 */ public static <T> T fastJsonToObject(String jsonStr,Class<T> mClass) { T mt = JSON.parseObject(jsonStr,mClass); return mt; } /** * 将Json字符串解析成对应的ArrayList<T>集合 * @param jsonStr 需要解析的Json字符串 * @param mType 需要解析成的Java对象类型 * @return mlList 解析后的ArrayList<T>集合 */ public static <T> ArrayList<T> fastJsonToListObjectOrString(String jsonStr,Class<T> mClass) { ArrayList<T> mList = (ArrayList<T>) JSON.parseArray(jsonStr, mClass); return mList; } /** * 将Json字符串解析成对应的ArrayList<Map<String,T>>集合 * @param jsonStr 需要解析的Json字符串 * @param mType 需要解析成的Java对象类型 * @return mapsList 解析后的ArrayList<Map<String,T>>集合 */ public static <T> ArrayList<Map<String, T>> fastJsonGetListMapObject(String jsonStr,TypeReference<T> mTypeReference) { ArrayList<Map<String, T>> mapsList = (ArrayList<Map<String, T>>) JSON.parseObject(jsonStr, mTypeReference); return mapsList; } /** * 获取需要转换的List<Map<String,T>>类型 * @param mClass * @return mType */ public static <T> TypeReference fastJsonGetetListMapTypeReference(Class<T> mClass){ TypeReference mTypeReference = null; if(mClass == Person.class){ mTypeReference = new TypeReference<ArrayList<Map<String, Person>>>(){}; }else if(mClass == String.class){ mTypeReference = new TypeReference<ArrayList<Map<String, String>>>(){}; } return mTypeReference; }然后在FastJsonActivity中编写展示解析完成的Json数据,具体的代码如下:
public class FastJsonActivity extends BaseActivity { @Override public void onDownloaded(String result, int typeId) { // TODO Auto-generated method stub ArrayList<String> mList = new ArrayList<String>(); switch (typeId) { case R.id.json_person: Person mPerson = JsonTools.fastJsonToObject(result, Person.class); mList.add(mPerson.toString()); break; case R.id.json_string: ArrayList<String> mArrayList = JsonTools.fastJsonToListObjectOrString(result, String.class); mList.addAll(mArrayList); break; case R.id.json_list_person: ArrayList<Person> mPersons = JsonTools.fastJsonToListObjectOrString(result, Person.class); mList.addAll(personsToString(mPersons)); break; case R.id.json_listmap_person: ArrayList<Map<String, Person>> maps = JsonTools.fastJsonGetListMapObject(result, JsonTools.fastJsonGetetListMapTypeReference(Person.class)); mList.addAll(listmappersonsToString(maps)); break; default: break; } bindData(mList); } }接下来便在Android模拟器上运行程序,我们来操作Gson、Jackson和Fastjson三种解析方式的运行效果,效果图如下: