1. 程式人生 > >Android中Json資料解析(二)--使用Gson、Jackson和FastJson解析Json資料

Android中Json資料解析(二)--使用Gson、Jackson和FastJson解析Json資料

/**-----------------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;
	}