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. Vxlan简介

    1.为什么需要Vxlan 1.什么是VXLAN VXLAN(Virtual eXtensible LAN可扩展虚拟局域网),是一种mac in UDP技术.传统的二层帧被封装到了UDP的报文中,通过U ...

  2. hdu 4960 记忆化搜索 DP

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  3. Ansible进阶

    YAML YAML简介 YAML是一个可读性高,并用来表达资料序列的格式.YAML参考了其它多种语言,包括:XML.C语言.Python.Perl以及电子邮件格式RFC2822等 它是一种直观的能够被 ...

  4. jQuery插件封装系列(一)—— 金额录入框

    基于jQuery原型封装数值录入框,禁止录入.粘贴非数值字符 (function ($) { // 数值输入框 $.fn.numbox = function (options) { var type ...

  5. android本地存储SharedPreferences

    SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...

  6. 激活win10系统的方法(亲测)

    WIN+X 按A (或者点击左下角有个windows小图标“鼠标右键”选择选择“命令提示符号(管理员)”) 输入下面命令,回车(一行按一个回车键)slmgr.vbs /upkslmgr /ipk W2 ...

  7. oracle学习笔记(十四) 数据库对象 索引 视图 序列 同义词

    数据库对象 用户模式:指数据库用户所创建和存储数据对象的统称.在访问其它用户模式的数据库对象时需加上用户模式. 如:scott.emp, scott.dept等. 数据库对象包括:表.视图.索引.序列 ...

  8. Ubuntu 16.04下快速在当前目录打开终端的快捷键设置

    说明:不一定每次都准确打开,80%的机会是行的. 原理:使用xdotool模拟键盘按键,打开的文件夹管理界面,然后通过Ctrl+L获取地址栏地址,然后传递到终端上. 安装: 1.安装xdotool s ...

  9. ADO.NET 对数据操作 以及如何通过C# 事务批量导入数据

    ADO.NET 对数据操作 以及如何通过C# 事务批量导入数据   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  10. PHP生成excel(1)

    先到PHPExcel官网下载PHPExcel类 http://phpexcel.codeplex.com/ 把excel类包含进来,然后直接使用 <?php require "./PH ...