JacksonJson 处理Json与对象之间的转换

来源:互联网 发布:干湿报警阀组的算法 编辑:程序博客网 时间:2024/06/12 23:05

又学到一种新的解析Json ,并且很容易与对象之间互转的工具类,记录备用。
首先记录两个查maven pom 的网站,比较好用:
http://search.maven.org/#search%7Cga%7C1%7Cmybatis
http://mvnrepository.com/

实体类package jsonTest;import java.util.Date;import com.fasterxml.jackson.annotation.JsonFormat;import com.fasterxml.jackson.annotation.JsonProperty;public class Student {    // @JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。    // @JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern =    // "yyyy-MM-dd HH-mm-ss")。    // @JsonProperty    // 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")   @JsonProperty("number")    private String id;    private String name;    private String age;    private String sex;    @JsonFormat(pattern ="yyyy年MM月dd日")    private Date birth;    public Date getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}
测试调用类package jsonTest;import java.io.IOException;import java.text.ParseException;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.fasterxml.jackson.databind.ObjectMapper;public class jacksonJson {        public static void main(String[] args) throws ParseException, IOException {              /**              * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。              * ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。              * writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。              * writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。              * writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。              * writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。              */              ObjectMapper mapper = new ObjectMapper();              System.out.println("***************单个对象转Json************************");            Student student=new Student();            student.setId("20123094");            student.setAge("22");            student.setName("Bin");            student.setSex("男");            Date date=new Date();            student.setBirth(date);            String studentJson=mapper.writeValueAsString(student);            System.out.println("studentJson="+studentJson);            System.out.println();            System.out.println("***************多个对象转Json************************");            List<Student> students=new ArrayList<Student>();            students.add(student);            students.add(student);            students.add(student);            String studentsJson=mapper.writeValueAsString(students);            System.out.println("studentsJson="+studentsJson);            System.out.println();            System.out.println("***************json转对象************************");            Student newStu=new Student();            newStu=mapper.readValue(studentJson, Student.class);            System.out.println("json转换后结果如下:");            System.out.println("newStu.getName()="+newStu.getName()+"|newStu.getSex()="+newStu.getSex());        }  }
测试结果输出:***************单个对象转Json************************studentJson={"name":"Bin","age":"22","sex":"男","birth":"2016年09月02日","number":"20123094"}***************多个对象转Json************************studentsJson=[{"name":"Bin","age":"22","sex":"男","birth":"2016年09月02日","number":"20123094"},{"name":"Bin","age":"22","sex":"男","birth":"2016年09月02日","number":"20123094"},{"name":"Bin","age":"22","sex":"男","birth":"2016年09月02日","number":"20123094"}]***************json转对象************************json转换后结果如下:newStu.getName()=Bin|newStu.getSex()=男
1 0