jackson序列化和反序列化(单个对象、列表)

1.maven配置
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.7.4</version>
</dependency>

2.新建一个person对象:
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

public class Person {

	@JsonProperty("n")//这个是标识序列化时用n来代替name
	private String name;
	
	private String sex;
	@JsonIgnore//这个是标识不对该属性进行序列化
	private int age;
	@JsonProperty("a")
	private String address;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	
}

3.整个测试类:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;


public class TestJson {

	private ObjectMapper objectMapper = new  ObjectMapper();
	
//单个对象的序列化
	public void testTojson(){
		Person testPerson = new Person();
		testPerson.setAddress("dsfsdf");
		testPerson.setAge(10);
		testPerson.setName("name");
		testPerson.setSex(null);
		try {
			String resultStr = objectMapper.writeValueAsString(testPerson);
			System.out.println(resultStr);
		} catch (JsonGenerationException 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();
		}
		
	}
	
//将json反序列化为对象
	public void testFromJson(){
		String content = "{\"n\":\"name\",\"sex\":\"sec\",\"a\":\"null\"}";
		try {
			Person resultPerson = objectMapper.readValue(content, Person.class);
			System.out.println(resultPerson.getAddress().trim());
			System.out.println(resultPerson.getAge());
		} 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();
		}
	}
	
//对象列表的序列化
	public void testFromList(){
		Person testPerson1 = new Person();
		testPerson1.setAddress("dsfsdf111");
		testPerson1.setAge(10);
		testPerson1.setName("name11");
		testPerson1.setSex("sec11");
		Person testPerson2 = new Person();
		testPerson2.setAddress("dsfsdf222");
		testPerson2.setAge(10);
		testPerson2.setName("name22");
		testPerson2.setSex("sec22");
		Person testPerson3 = new Person();
		testPerson3.setAddress("dsfsdf333");
		testPerson3.setAge(10);
		testPerson3.setName("name33");
		testPerson3.setSex("sec33");
		try {
			List<Person> personList = new ArrayList<Person>();
			personList.add(testPerson1);
			personList.add(testPerson2);
			personList.add(testPerson3);
			String resultStr = objectMapper.writeValueAsString(personList);
			System.out.println(resultStr);
		} catch (JsonGenerationException 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();
		}
	}
	
//将josn串反序列化为对象列表
	public void testListFromJson(){
		String content = "[{\"sex\":\"sec11\",\"n\":\"name11\",\"a\":\"dsfsdf111\"}," +
				"{\"sex\":\"sec22\",\"n\":\"name22\",\"a\":\"dsfsdf222\"}," +
				"{\"sex\":\"sec33\",\"n\":\"name33\",\"a\":\"dsfsdf333\"}]";
		try {
			//这个有问题
//			List<Person> resultPerson = objectMapper.readValue(content, ArrayList.class);
//			System.out.println(resultPerson.get(0).getAddress());
			List<Person> d = objectMapper.readValue(content, new TypeReference<List<Person>>(){});
			System.out.println(d.get(0).getAddress());
		} 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();
		}
	}
	
	public static void main(String[] args) {
		TestJson test = new TestJson();
		test.testTojson();
		test.testFromJson();
		test.testFromList();
		test.testListFromJson();
	}
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值