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(); } }