Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1747151
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-04-08 10:57:32

Jackson 的地址:
document 非常多:
下载地址:
从这里下载3个jar包, databind,core, annotations.
然后从 下载dataform-xml 这个jar包.

下面的两个例子是抄自: %E5%88%A9%E7%94%A8-jackson

点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import com.fasterxml.jackson.databind.ObjectMapper;


  8. class User{
  9.     private String name;
  10.     private Integer age;
  11.     private Date birthday;
  12.     private String email;
  13.     
  14.     public User(){
  15.     }
  16.     
  17.     public User(String name, Integer age, Date birthday,String email){
  18.         this.name = name;
  19.         this.age = age;
  20.         this.birthday = birthday;
  21.         this.email = email;
  22.     }
  23.     
  24.     public String getName() {
  25.         return name;
  26.     }
  27.     public void setName(String name) {
  28.         this.name = name;
  29.     }
  30.     public Integer getAge() {
  31.         return age;
  32.     }
  33.     public void setAge(Integer age) {
  34.         this.age = age;
  35.     }
  36.     public Date getBirthday() {
  37.         return birthday;
  38.     }
  39.     public void setBirthday(Date birthday) {
  40.         this.birthday = birthday;
  41.     }
  42.     public String getEmail() {
  43.         return email;
  44.     }
  45.     public void setEmail(String email) {
  46.         this.email = email;
  47.     }
  48. }

  49. public class JacksonExample2 {
  50.     

  51.     
  52.     //static JacksonExample2 x= new JacksonExample2();
  53.     //java对象转Json
  54.     
  55.     public static void obj2Json() throws ParseException, IOException{
  56.         User user = new User();
  57.         user.setName("小民");
  58.         user.setEmail("xiaomin@sina.com");
  59.         user.setAge(20);
  60.         
  61.         SimpleDateFormat datefmt = new SimpleDateFormat("yyyy-MM-dd");
  62.         user.setBirthday(datefmt.parse("1996-10-01"));
  63.          /**
  64.          * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
  65.          * ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
  66.          * writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
  67.          * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
  68.          * writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
  69.          * writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
  70.          */
  71.         ObjectMapper mapper = new ObjectMapper();
  72.         //User类转JSON
  73.         //输出结果: {"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
  74.         String json = mapper.writeValueAsString(user);
  75.         System.out.println(json);
  76.         
  77.         //Java集合转JSON
  78.         List<User> users = new ArrayList<>();
  79.         users.add(user);
  80.         String jsonlist = mapper.writeValueAsString(users);
  81.         System.out.println(jsonlist);
  82.     }

  83.     //JSON转Java类[JSON反序列化]
  84.     public static void JSON2Class()throws ParseException, IOException{
  85.         String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";
  86.          /**
  87.          * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
  88.          */
  89.         ObjectMapper mapper = new ObjectMapper();
  90.         User user = mapper.readValue(json, User.class);
  91.         System.out.println(user);
  92.     }
  93.     
  94.     public static void main(String[] args)throws ParseException, IOException {
  95.         // TODO Auto-generated method stub
  96.         obj2Json();
  97.         JSON2Class();
  98.     }

  99. }
还是来自同样的网站

点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;

  5. import com.fasterxml.jackson.annotation.JsonFormat;
  6. import com.fasterxml.jackson.annotation.JsonIgnore;
  7. import com.fasterxml.jackson.annotation.JsonProperty;
  8. import com.fasterxml.jackson.databind.ObjectMapper;

  9. /*
  10. 四、JSON注解
  11. Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解。
  12. @JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。
  13. @JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。
  14. @JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。
  15. */

  16. class User2{
  17.     private String name;
  18.     
  19.     //不JSON序列化年龄属性
  20.     @JsonIgnore
  21.     private Integer age;
  22.     
  23.     //格式化日期属性
  24.     @JsonFormat(pattern="yyyy年MM月dd日")
  25.     private Date birthday;
  26.     
  27.     //序列化email为mail
  28.     @JsonProperty("mail")
  29.     private String email;

  30.     public String getName() {
  31.         return name;
  32.     }

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

  36.     public Integer getAge() {
  37.         return age;
  38.     }

  39.     public void setAge(Integer age) {
  40.         this.age = age;
  41.     }

  42.     public Date getBirthday() {
  43.         return birthday;
  44.     }

  45.     public void setBirthday(Date birthday) {
  46.         this.birthday = birthday;
  47.     }

  48.     public String getEmail() {
  49.         return email;
  50.     }

  51.     public void setEmail(String email) {
  52.         this.email = email;
  53.     }
  54. }
  55. public class JacksonExample3 {
  56.     
  57.     
  58.     public static void main(String[] args) throws ParseException, IOException{
  59.         // TODO Auto-generated method stub
  60.         User2 user = new User2();
  61.         user.setName("小民");
  62.         user.setEmail("xiaomin@sina.com");
  63.         user.setAge(20);
  64.         
  65.         SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  66.         user.setBirthday(fmt.parse("1996-10-01"));
  67.         
  68.         ObjectMapper mapper = new ObjectMapper();
  69.         String json = mapper.writeValueAsString(user);
  70.         System.out.println(json);
  71.     }

  72. }



阅读(1080) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~