javabean与json转换(fastjson与jackson两个版本)
Posted PacosonSWJTU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javabean与json转换(fastjson与jackson两个版本)相关的知识,希望对你有一定的参考价值。
【README】
本文演示了 javabean与json转换的开发方式;
要想 javabean的属性名 与 json的字段名不一致,也是可以转换的;
之前需要引入 ali.fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
【1】javabean与fastjson互转
对于 perons, 需求是属性id 转换为 json字符串后,其字段名为 myid;
public class FastPerson
@JSONField(name="myid") // bean属性id的别名为myid
private int id;
@JSONField(name="name")
private String name;
@JSONField(serialize = false) // bean的city属性不转为json字符串
private String city;
@JSONField(deserialize = false) // json字符串的addr字段不解析到bean的addr属性
private String addr;
// 不进行序列化与反序列化(transient对 fastjson有效)
private transient String dontTrans;
public void setId(int id)
this.id = id;
public void setName(String name)
this.name = name;
public void setCity(String city)
this.city = city;
public void setAddr(String addr)
this.addr = addr;
public int getId()
return id;
public String getName()
return name;
public String getCity()
return city;
public String getAddr()
return addr;
public String getDontTrans()
return dontTrans;
public void setDontTrans(String dontTrans)
this.dontTrans = dontTrans;
@Override
public String toString()
return "FastPerson" +
"id=" + id +
", name='" + name + '\\'' +
", city='" + city + '\\'' +
", addr='" + addr + '\\'' +
", dontTrans1='" + dontTrans + '\\'' +
'';
转换测试用例;
public class FastJsonDemo
public static void main(String[] args)
FastPerson person2 = new FastPerson();
person2.setId(1);
person2.setName("zhangsan");
person2.setAddr("gaoxinqu");
person2.setCity("cd");
person2.setDontTrans("dont");
// bean 转json字符串
String jsonstr2 = JSON.toJSONString(person2);
System.out.println(jsonstr2);
// json字符串转bean
FastPerson p2 = JSON.parseObject(jsonstr2, FastPerson.class);
System.out.println(p2);
打印结果;
"addr":"gaoxinqu","myid":1,"name":"zhangsan"
Person2id=1, name='zhangsan', city='null', addr='null', dontTrans1='null'
【补充】
- @JSONField(name="myid") 注解用于定义javabean属性的别名,即转为json字符串之后的字段名;
- @JSONField(serialize = false) bean的city属性不转为json字符串(不进行序列化)
- @JSONField(deserialize = false) json字符串的addr字段不解析到bean的addr属性(不进行反序列化)
- transient 用于表示该字段不进行序列化与反序列化;
- 需要转换json的bean,必须要有 getter 和 setter 方法 ;
【2】javabean与jackson互转
添加maven依赖;
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
jackson测试的javabean;
public class JacksonPerson
@JsonProperty("myid") // bean属性id的别名为myid
private int id;
@JsonProperty("name")
private String name;
@JsonIgnore // bean的city属性不转为json字符串
private String city;
@JsonIgnore // json字符串的addr字段不解析到bean的addr属性
private String addr;
// transient 对 jackson 无效, 即jackson还是会序列化与反序列化 dontTrans 属性
private transient String dontTrans;
public void setId(int id)
this.id = id;
public void setName(String name)
this.name = name;
public void setCity(String city)
this.city = city;
public void setAddr(String addr)
this.addr = addr;
public int getId()
return id;
public String getName()
return name;
public String getCity()
return city;
public String getAddr()
return addr;
public String getDontTrans()
return dontTrans;
public void setDontTrans(String dontTrans)
this.dontTrans = dontTrans;
@Override
public String toString()
return "JacksonPerson" +
"id=" + id +
", name='" + name + '\\'' +
", city='" + city + '\\'' +
", addr='" + addr + '\\'' +
", dontTrans1='" + dontTrans + '\\'' +
'';
jackson测试用例:
public class JacksonDemo
public static void main(String[] args)
JacksonPerson jacksonPerson = new JacksonPerson();
jacksonPerson.setId(1);
jacksonPerson.setName("zhangsan");
jacksonPerson.setAddr("gaoxinqu");
jacksonPerson.setCity("cd");
jacksonPerson.setDontTrans("dont");
// bean 转json字符串
String jacksonStr = JacksonUtils.beanToJson(jacksonPerson);
System.out.println(jacksonStr);
// json字符串转bean
JacksonPerson p2 = JacksonUtils.jsonToBean(jacksonStr, JacksonPerson.class);
System.out.println(p2);
// jackson 工具
public class JacksonUtils
private JacksonUtils()
private static ObjectMapper objectMapper;
static
objectMapper = new ObjectMapper();
//bean转换为json字符串
public static String beanToJson(Object bean)
String jsonStr = "";
try
jsonStr = objectMapper.writeValueAsString(bean);
catch (JsonProcessingException e)
e.printStackTrace();
return jsonStr;
//map转换为json字符串
public static String mapToJson(Map<String, Object> map)
String jsonStr = "";
try
jsonStr = objectMapper.writeValueAsString(map);
catch (JsonProcessingException e)
e.printStackTrace();
return jsonStr;
//list转换为json字符串
public static String listToJson(List<Object> list)
String jsonStr = "";
try
jsonStr = objectMapper.writeValueAsString(list);
catch (JsonProcessingException e)
e.printStackTrace();
return jsonStr;
//json字符串转换为bean
public static <T> T jsonToBean(String jsonStr, Class<T> clazz)
T bean = null;
try
bean = objectMapper.readValue(jsonStr, clazz);
catch (IOException e)
e.printStackTrace();
return bean;
//json字符串转换为list
public static <T> List<T> jsonToList(String jsonStr, Class<T> clazz)
List list = null;
try
list = (List) objectMapper.readValue(jsonStr, clazz);
catch (IOException e)
e.printStackTrace();
return list;
//json字符串转换为map
public static Map jsonToMap(String jsonStr, Class<?> clazz)
Map map = null;
try
map = (Map) objectMapper.readValue(jsonStr, clazz);
catch (IOException e)
e.printStackTrace();
return map;
打印结果:
"dontTrans":"dont","myid":1,"name":"zhangsan"
JacksonPersonid=1, name='zhangsan', city='null', addr='null', dontTrans1='dont'
显然, transient 对 jackson 无效,即 即jackson还是会序列化与反序列化 dontTrans 属性;
【2】fastjson 与 jackson 区别
【2.1】fastjson
是阿里的开源框架,被不少企业使用,是一个极其优秀的Json框架; 字符串解析成JavaBean时,当数据量较少时首选fastjson;字符串解析成JSON时,当数据量较少时首选fastjson。
例子:
//将对象或List转为json字符串或json数组
JSON.toJSONString(obj);
//将json字符串和json数组转为对象或list
JSON.toJSON(jsonStr)
【2.2】jackson
是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”;字符串解析成JavaBean时,数据量较大使用jackson;字符串解析成JSON时,数据量较大使用jackson。
//首先要先定义一个Mapper
ObjectMapper mapper = new ObjectMapper();
//将对象或List转为json字符串或json数组
mapper.writeValueAsString(obj);
//将json字符串和json数组转为对象或list
mapper.readValue(jsonStr, Class);
补充: 数据量是多少算较少,算较大 ?
本文仅给出一个经验值 100w,但具体要在本地做性能测试才可以计算出来;
补充:两者性能测试 refer2
[享学Jackson] 二十二、Jackson与Fastjson的恩怨情仇(完结篇)【附源码】_YourBatman_51CTO博客
以上是关于javabean与json转换(fastjson与jackson两个版本)的主要内容,如果未能解决你的问题,请参考以下文章
FastJson对于JSON格式字符串JSON对象及JavaBean之间的相互转换
FastJson对于JSON格式字符串JSON对象及JavaBean之间的相互转换
FastJson对于JSON格式字符串JSON对象及JavaBean之间的相互转换