java操作Json工具类

17 篇文章 0 订阅
13 篇文章 0 订阅
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();
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值