jackson序列化和反序列化(单个对象、列表)
2014-02-12 18:37
218 查看
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(); } }
相关文章推荐
- jackson序列化和反序列化(单个对象、列表)
- Jackson 自定义序列化 & 反序列化 对象类型
- jackson实现简单对象的序列化,反序列化
- XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置
- 对象的序列化和反序列化
- Java-对象的序列化和反序列化
- Java对象的序列化和反序列化
- Java对象的序列化和反序列化及其实现和使用
- .NET对象的XML序列化和反序列化
- objective-c自定义类对象的序列化和反序列化
- Java对象的序列化和反序列化
- java对象的序列化和反序列化
- Java对象的序列化和反序列化
- Java对象的序列化和反序列化
- c#.net将对象序列化,反序列化json
- 对象的序列化和反序列化
- 将java对象存储到redis数据库(使用序列化和反序列化)
- 谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter、SoapFormatter、XmlSerializer还是BinaryReader
- 对象的序列化与反序列化,以xml,二进制流的方式,soap方式序列化对象
- Protostuff 序列化/反序列化集合等对象