1,User对象

package com.st.json;

import java.util.Date;

/**
* @Description: JSON序列化和反序列化使用的User类
* @author Mr.Li
* @date 2018年4月21日 下午10:55:34
*/
public class User { private Integer id;
private String name;
private Integer age;
private Date birthday;
private String email; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", birthday=" + birthday + ", email=" + email
+ "]";
} public User(Integer id, String name, Integer age, Date birthday, String email) {
super();
this.id = id;
this.name = name;
this.age = age;
this.birthday = birthday;
this.email = email;
} public User() {
super();
// TODO Auto-generated constructor stub
} }

  2,JacksonUtil 工具类

package com.st.json;

import java.io.IOException;
import java.io.StringWriter; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; /**
* @Description: Jackson 对象与json互转工具类
* @author Mr.Li
* @date 2018年4月22日 上午12:30:15
*/
public class JacksonUtil { private static ObjectMapper mapper = new ObjectMapper(); public static String BeanToJson(Object obj) throws IOException {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(sw);
mapper.writeValue(gen, obj);
gen.close();
return sw.toString();
} public static <T> T JsonToBean(String jsonStr, Class<T> objClass)
throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(jsonStr, objClass);
} }

  3,Jackson 对象与json互转测试

package com.st.json;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException; /**
* @Description: Jackson 对象与json互转测试
* @author Mr.Li
* @date 2018年4月22日 上午12:39:39
*/
public class JacksonUtilTest { public static void main(String[] args) throws ParseException, IOException {
beanTojack();
// jsonTobean();
} public static void beanTojack() throws ParseException, IOException { User u = new User();
u.setId(1);
u.setName("curry");
u.setAge(30);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
u.setBirthday(dateFormat.parse("1988-9-21"));
u.setEmail("138@163.com"); User u2 = new User();
u2.setId(2);
u2.setName("KD");
u2.setAge(29);
u2.setBirthday(dateFormat.parse("1989-9-21"));
u2.setEmail("123@qq.com"); List<User> users = new ArrayList<>();
users.add(u);
users.add(u2); //对象转json格式
String beanToJson = JacksonUtil.BeanToJson(users);
System.out.println(beanToJson);
} public static void jsonTobean() throws JsonParseException, JsonMappingException, IOException {
String json = " {\"id\":3, \"name\":\"小明\", \"age\":18, \"birthday\":590774400000, \"email\":\"xiaomin@sina.com\"} "; User jsonToBean = JacksonUtil.JsonToBean(json, User.class);
System.out.println(jsonToBean);
} }

Jackson 对象与json数据互转工具类JacksonUtil的更多相关文章

  1. 利用jackson-databind,复杂对象对象和json数据互转

    如果简单对象,那么转换的方式比较多,这里指的复杂对象,是指对象里面存在cycle引用,比如: /** * @author ding * */@Entity@Table(name = "ser ...

  2. Java中对象和json互相转换的工具类

    package com.Dingyu.util; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExce ...

  3. json数据与Gson工具类的使用

    JS中使用JSON JSON对象 --> JSON字符串:JSON.stringify(对象) JSON字符串 --> JSON对象:JSON.parse(JSON字符串) <scr ...

  4. 总结的一些json格式和对象/String/Map/List等的互转工具类

    总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...

  5. JAVA写JSON的三种方法,java对象转json数据

    JAVA写JSON的三种方法,java对象转json数据 转自:http://www.xdx97.com/#/single?bid=5afe2ff9-8cd1-67cf-e7bc-437b74c07a ...

  6. java中map和对象互转工具类的实现示例

    在项目开发中,经常碰到map转实体对象或者对象转map的场景,工作中,很多时候我们可能比较喜欢使用第三方jar包的API对他们进行转化,而且用起来也还算方便,比如像fastJson就可以轻松实现map ...

  7. C#的百度地图开发(二)转换JSON数据为相应的类

    原文:C#的百度地图开发(二)转换JSON数据为相应的类 在<C#的百度地图开发(一)发起HTTP请求>一文中我们向百度提供的API的URL发起请求,并得到了返回的结果,结果是一串JSON ...

  8. 将Json数据 填充到 实例类 的函数

    /// <summary> /// 将Json数据 填充到 实例类 /// </summary> /// <typeparam name="T"> ...

  9. JavaScript 数据值校验工具类

    /** * 数据值校验工具类 */ var checkService = { // 不校验 none: function () { return true; }, //非空校验 isEmpty: fu ...

随机推荐

  1. farm

    farm 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 White Rabbit has ...

  2. Heritage of skywalkert

    Heritage of skywalkert skywalkert, the new legend of Beihang University ACM-ICPC Team, retired this ...

  3. ElasticSearch 多索引

    1.用逗号将索引隔开,如: $ curl -XPOST http://localhost:9200/aaa,website/_search/ { "took": 1, " ...

  4. readonly和disabled的异同

    好久木有写博客了,今来逛逛 话说今天搞form表单的时候,主管让俺把手机号设成只读的.当时我就...咳咳,然后我就问了下万能的百度君,果断还是有解决方法的嘛,那么,今就谈谈readonly和disab ...

  5. 标准C程序设计七---07

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  6. ubuntu 14.04安装nodejs

    http://stackoverflow.com/questions/32902699/cannot-install-ember-on-ubuntu-1404/33495134

  7. 转:Python yield 使用浅析 from IBM Developer

    评注:没有看懂. 转: https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ Python yield 使用浅析 初 ...

  8. 前端编程提高之旅(十)----表单验证插件与cookie插件

        实际项目开发中与用户交互的常见手法就是採用表单的形式.取得用户注冊.登录等信息.而当用户注冊或登录后又须要记住用户的登录状态.这就涉及到经常使用的两个操作:表单验证与cookie增删查找.   ...

  9. react 实现pure render的时候,bind(this)隐患

    react 实现pure render的时候,bind(this)隐患 export default class Parent extends Component { ... render() { c ...

  10. Flex4_操作XML

    一.创建XML类 1.把字符串对象转换为XML: var xmlStr:String="<students><student><name>吕布</n ...