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

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 1.maven配置  
  2. <dependency>  
  3. <groupId>org.codehaus.jackson</groupId>  
  4. <artifactId>jackson-mapper-lgpl</artifactId>  
  5. <version>1.7.4</version>  
  6. </dependency>  

2.新建一个person对象:
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import org.codehaus.jackson.annotate.JsonIgnore;  
  2. import org.codehaus.jackson.annotate.JsonProperty;  
  3.   
  4. public class Person {  
  5.   
  6.     @JsonProperty("n")//这个是标识序列化时用n来代替name  
  7.     private String name;  
  8.       
  9.     private String sex;  
  10.     @JsonIgnore//这个是标识不对该属性进行序列化  
  11.     private int age;  
  12.     @JsonProperty("a")  
  13.     private String address;  
  14.   
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.   
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.   
  23.     public String getSex() {  
  24.         return sex;  
  25.     }  
  26.   
  27.     public void setSex(String sex) {  
  28.         this.sex = sex;  
  29.     }  
  30.   
  31.     public int getAge() {  
  32.         return age;  
  33.     }  
  34.   
  35.     public void setAge(int age) {  
  36.         this.age = age;  
  37.     }  
  38.   
  39.     public String getAddress() {  
  40.         return address;  
  41.     }  
  42.   
  43.     public void setAddress(String address) {  
  44.         this.address = address;  
  45.     }  
  46.       
  47.       
  48. }  

3.整个测试类:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4.   
  5. import org.codehaus.jackson.JsonGenerationException;  
  6. import org.codehaus.jackson.JsonParseException;  
  7. import org.codehaus.jackson.map.JsonMappingException;  
  8. import org.codehaus.jackson.map.ObjectMapper;  
  9. import org.codehaus.jackson.type.TypeReference;  
  10.   
  11.   
  12. public class TestJson {  
  13.   
  14.     private ObjectMapper objectMapper = new  ObjectMapper();  
  15.       
  16. //单个对象的序列化  
  17.     public void testTojson(){  
  18.         Person testPerson = new Person();  
  19.         testPerson.setAddress("dsfsdf");  
  20.         testPerson.setAge(10);  
  21.         testPerson.setName("name");  
  22.         testPerson.setSex(null);  
  23.         try {  
  24.             String resultStr = objectMapper.writeValueAsString(testPerson);  
  25.             System.out.println(resultStr);  
  26.         } catch (JsonGenerationException e) {  
  27.             // TODO Auto-generated catch block  
  28.             e.printStackTrace();  
  29.         } catch (JsonMappingException e) {  
  30.             // TODO Auto-generated catch block  
  31.             e.printStackTrace();  
  32.         } catch (IOException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         }  
  36.           
  37.     }  
  38.       
  39. //将json反序列化为对象  
  40.     public void testFromJson(){  
  41.         String content = "{\"n\":\"name\",\"sex\":\"sec\",\"a\":\"null\"}";  
  42.         try {  
  43.             Person resultPerson = objectMapper.readValue(content, Person.class);  
  44.             System.out.println(resultPerson.getAddress().trim());  
  45.             System.out.println(resultPerson.getAge());  
  46.         } catch (JsonParseException e) {  
  47.             // TODO Auto-generated catch block  
  48.             e.printStackTrace();  
  49.         } catch (JsonMappingException e) {  
  50.             // TODO Auto-generated catch block  
  51.             e.printStackTrace();  
  52.         } catch (IOException e) {  
  53.             // TODO Auto-generated catch block  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57.       
  58. //对象列表的序列化  
  59.     public void testFromList(){  
  60.         Person testPerson1 = new Person();  
  61.         testPerson1.setAddress("dsfsdf111");  
  62.         testPerson1.setAge(10);  
  63.         testPerson1.setName("name11");  
  64.         testPerson1.setSex("sec11");  
  65.         Person testPerson2 = new Person();  
  66.         testPerson2.setAddress("dsfsdf222");  
  67.         testPerson2.setAge(10);  
  68.         testPerson2.setName("name22");  
  69.         testPerson2.setSex("sec22");  
  70.         Person testPerson3 = new Person();  
  71.         testPerson3.setAddress("dsfsdf333");  
  72.         testPerson3.setAge(10);  
  73.         testPerson3.setName("name33");  
  74.         testPerson3.setSex("sec33");  
  75.         try {  
  76.             List<Person> personList = new ArrayList<Person>();  
  77.             personList.add(testPerson1);  
  78.             personList.add(testPerson2);  
  79.             personList.add(testPerson3);  
  80.             String resultStr = objectMapper.writeValueAsString(personList);  
  81.             System.out.println(resultStr);  
  82.         } catch (JsonGenerationException e) {  
  83.             // TODO Auto-generated catch block  
  84.             e.printStackTrace();  
  85.         } catch (JsonMappingException e) {  
  86.             // TODO Auto-generated catch block  
  87.             e.printStackTrace();  
  88.         } catch (IOException e) {  
  89.             // TODO Auto-generated catch block  
  90.             e.printStackTrace();  
  91.         }  
  92.     }  
  93.       
  94. //将josn串反序列化为对象列表  
  95.     public void testListFromJson(){  
  96.         String content = "[{\"sex\":\"sec11\",\"n\":\"name11\",\"a\":\"dsfsdf111\"}," +  
  97.                 "{\"sex\":\"sec22\",\"n\":\"name22\",\"a\":\"dsfsdf222\"}," +  
  98.                 "{\"sex\":\"sec33\",\"n\":\"name33\",\"a\":\"dsfsdf333\"}]";  
  99.         try {  
  100.             //这个有问题  
  101. //          List<Person> resultPerson = objectMapper.readValue(content, ArrayList.class);  
  102. //          System.out.println(resultPerson.get(0).getAddress());  
  103.             List<Person> d = objectMapper.readValue(content, new TypeReference<List<Person>>(){});  
  104.             System.out.println(d.get(0).getAddress());  
  105.         } catch (JsonParseException e) {  
  106.             // TODO Auto-generated catch block  
  107.             e.printStackTrace();  
  108.         } catch (JsonMappingException e) {  
  109.             // TODO Auto-generated catch block  
  110.             e.printStackTrace();  
  111.         } catch (IOException e) {  
  112.             // TODO Auto-generated catch block  
  113.             e.printStackTrace();  
  114.         }  
  115.     }  
  116.       
  117.     public static void main(String[] args) {  
  118.         TestJson test = new TestJson();  
  119.         test.testTojson();  
  120.         test.testFromJson();  
  121.         test.testFromList();  
  122.         test.testListFromJson();  
  123.     }  
  124. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值