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
- JacksonJson 处理Json与对象之间的转换
- 用jackson包实现json、对象、Map之间的转换 博客分类: java jacksonjson 主要用于测试学习用jackson包实现json、对象、Map之间的转换。 1、准备测试用的Ja
- JSON 与JAVA对象之间的转换
- json对象与字符串之间的转换
- JSON 与JAVA对象之间的转换
- JSON对象与字符串之间的转换
- 对象与json文件之间的转换
- JSON对象与字符串之间的转换
- JSON 与 对象 、集合 之间的转换
- JSON 与 对象 、集合 之间的转换
- JSON 与 对象 、集合 之间的转换
- JSON 与 对象 、集合 之间的转换
- JSON 与 对象 、集合 之间的转换
- java对象与json之间的转换
- JSON 与 对象 、集合 之间的转换
- JSON 与 对象 、集合 之间的转换
- Json串与json对象之间的转换
- 补充:json对象与json字符串之间的相互转换
- adb 命令模拟按键事件
- npm ERR! network connect ETIMEDOUT
- Java类加载机制
- GPU加速will-change
- android逆向1
- JacksonJson 处理Json与对象之间的转换
- POJ 3277 City Horizon 矩形切割
- 学习笔记-unix软链接和硬链接的区别
- 第12篇.Recycleview
- greenplum 待看
- vc几个小技巧
- SD_AutoLayout cell自适应高度及方法大全
- 有章法的学习c++(1)编写一个简单的c++程序
- 赛码在线编程 2268 字符判断