Object2Json

话不多说,先封装下下XXX... 这里省略了N多XXX... 

package org.my.test.json.demo;

import java.io.IOException;
import java.io.StringWriter;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
 * 
 * @author spring
 *
 */
public class JsonUtil {
	private static JsonUtil ju;
	private static JsonFactory jf;
	private static ObjectMapper mapper;
	private JsonUtil(){}
	
	public static JsonUtil getInstance() {
		if(ju==null) {
			ju = new JsonUtil();
		}
		return ju;
	}
	
	public static ObjectMapper getMapper() {
		if(mapper==null) {
			mapper = new ObjectMapper();
		}
		return mapper;
	}
	
	public static JsonFactory getFactory() {
		if(jf==null) {
			jf = new JsonFactory();
		}
		return jf;
	}
	
	public String obj2json(Object obj) {
		JsonGenerator jg = null;
		try {
			jf = getFactory();
			mapper = getMapper();
			mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
			StringWriter out = new StringWriter();
			jg = jf.createGenerator(out);
			mapper.writeValue(jg, obj);
			return out.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(jg!=null) jg.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public Object json2obj(String json,Class<?> clz) {
		try {
			mapper = getMapper();
			return mapper.readValue(json,clz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

<span style="color:#ff0000;background-color: rgb(102, 102, 102);">这里是测试方法...</span>

package org.my.test.json.demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.my.test.entity.MyData;
import org.my.test.entity.Person;

import com.thoughtworks.xstream.XStream;


public class TestJaskson {
	
	@Test
	public void Object2Json() throws Exception {
		List<Person> us = new ArrayList<Person>();
		
		Person a = new Person();
		a.setId(new Long(222));
		a.setAge(22);
		a.setPassword("213aa");
		us.add(a);
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("sEch", "1");
		map.put("isisis", "2");
		map.put("aaData", us);

		System.out.println(JsonUtil.getInstance().obj2json(map));
	}

	@Test
	public void Object2Json_() throws Exception {
		List<Person> us = new ArrayList<Person>();
		
		Person a = new Person();
		a.setId(new Long(222));
		a.setAge(22);
		a.setPassword("213aa");
		us.add(a);
		MyData<Person> m = new MyData<Person>();
		m.setLists(us);
		m.setName("aa");
		m.setType("22");
		
		System.out.println(JsonUtil.getInstance().obj2json(m));
	}
	
	@Test
	public void Object2XML() throws Exception {
		XStream stream = new XStream();
		Person a = new Person();
		a.setId(new Long(222));
		a.setAge(22);
		a.setPassword("213aa");
		stream.alias("user", Person.class);
		System.out.println(stream.toXML(a));
	}
	
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值