jacksonUtil
jackson对json的转换
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
		
	public static String mapToJon(Object object) throws JsonProcessingException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(object);
	}
	
	public static String ListToJon(List list) throws JsonProcessingException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(list);
	}
	
	public static String objectToJson(Object object)throws JsonProcessingException
	{
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(object);
	}
	
		public static void main(String args[]) throws Exception, JsonMappingException, IOException
		{
			ObjectMapper mapper = new ObjectMapper();
	//		Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
		
			
		/*  将java对象装换成json数据*/
		/*	User u=new User();
			u.setAge("23");
			u.setName("xx");
			u.getHobby().add("pingbing");
			u.getHobby().add("dd");
			u.getHobby().add("e");
			u.getPartner().put("Name", "ss");
			u.getPartner().put("Name1", "ss");
			u.getPartner().put("Name2", "ss");
			System.out.println(mapper.writeValueAsString(u));*/
			
		/*	将json装换成pojo
			String s="{\"name\":\"xx\",\"age\":\"23\",\"hobby\":[\"pingbing\",\"dd\",\"e\"],\"partner\":{\"Name\":\"ss\",\"Name2\":\"ss\",\"Name1\":\"ss\"}}";
			User user=mapper.readValue(s, User.class);
			System.out.println(user.getAge()+user.getHobby().toString()+user.getPartner().toString());*/
		
		/*	map 转换成json
			Map<String,Object> map=new HashMap<String,Object>();
			Map<String,Object> map2=new HashMap<String,Object>();
			map2.put("currentPage", "3");
			map2.put("size", "10");
			map2.put("total", "10");
			map.put("name","d");
			map.put("pageInfo", map2);
			System.out.println(mapper.writeValueAsString(map));*/

		/*	json 装换成map
			String s="{\"pageInfo\":{\"total\":\"10\",\"currentPage\":\"3\",\"size\":\"10\"},\"name\":\"d\"}";
			Map map=mapper.readValue(s,Map.class );
			Map map2=(Map) map.get("pageInfo");
			System.out.println(map2);*/
			
			Map<String,Object> map=new HashMap<String,Object>();
			Map<String,Object> map2=new HashMap<String,Object>();
			map2.put("currentPage", "3");
			map2.put("size", "10");
			map2.put("total", "10");
			map.put("name","d");
			List list=new ArrayList();
			list.add(map2);
			map.put("pageInfo", list);
			
			System.out.println(mapper.writeValueAsString(list));
		}
}

阅读更多
个人分类: json
想对作者说点什么? 我来说一句

Jackson JSON框架的Util化封装

2012年12月04日 1.41MB 下载

JacksonUtil及Jar包1.9

2014年04月30日 1.84MB 下载

没有更多推荐了,返回首页

不良信息举报

jacksonUtil

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭