java操作Json工具類

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.JavaType;

public class JsonUtil {

	public final static ObjectMapper mapper = new ObjectMapper();
	
	static{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(df);
		mapper.setSerializationInclusion(Inclusion.NON_NULL);
		mapper.configure(Feature.WRITE_NUMBERS_AS_STRINGS, false);
		mapper.configure(Feature.WRITE_NUMBERS_AS_STRINGS, false);
	}
	
	private static JavaType getCollectionType(Class<?> collectionClass,Class<?>...classes){
		return mapper.getTypeFactory().constructParametricType(collectionClass, classes);
	}
	
	/**
	 * json字符串轉對象
	 * @param json
	 * @param c
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static <T> T jsonToObject(String json,Class<T> c) throws
	JsonParseException, JsonMappingException, IOException{
		if(json != null && !"".equals(json)){
			return mapper.readValue(json,c);
		}
		return null;
	}
	
	/**
	 * json字符串轉泛型集合
	 * @param json
	 * @param c
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static <T> List<T> jsonToObjectList(String json,Class<T> c) throws
	JsonParseException, JsonMappingException, IOException{
		List<T> result = null;
		if(json != null && !"".equals(json)){
			result = mapper.readValue(json,getCollectionType(ArrayList.class, c));
		}
		return result;
	}
	
	/**
	 * json字符串轉Map
	 * @param json
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static Map jsonToMap(String json) 
			throws JsonParseException, JsonMappingException, IOException{
		return jsonToObject(json, Map.class);
	}
	
	/**
	 * 集合轉json字符串
	 * @param list
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static String objectListToString(List list) 
			throws JsonGenerationException, JsonMappingException, IOException{
		if(list != null && list.size()>0){
			return mapper.writeValueAsString(list);
		}
		return null;
	}
	
	/**
	 * 對象轉json字符串
	 * @param o
	 * @return
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static String objectToString(Object o)
			throws JsonGenerationException, JsonMappingException, IOException{
		if(o!=null){
			return mapper.writeValueAsString(o);
		}
		return "";
	}
	public static void main(String[] args) {
		String s = "[{\"id\":\"0\",\"child\":[{\"id\":\"01\",\"name\":\"Tom\"},{\"id\":\"02\"}]},"
				+ "{\"id\":\"1\",\"name\":\"Jerry\"}]";
		try {
			List<Map> list = JsonUtil.jsonToObjectList(s, Map.class);
			System.out.println(list);//[{id=0, child=[{id=01, name=Tom}, {id=02}]}, {id=1, name=Jerry}]
			for(Map<String,Object> map : list){
				System.out.println(map.get("id"));//0  1
			}
		} 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();
		}
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章