springmvc下对象转json格式

类别:javaee  阅读:363  发布时间:Sun May 08 18:59:13 CST 2016

导入jackson包,使用maven管理

<jackson-mapper-asl-version>1.9.12</jackson-mapper-asl-version>
<jackson-core-asl-version>1.9.12</jackson-core-asl-version>

代码:

//springmvc转对象json格式,在jackson包下
ObjectMapper om = new ObjectMapper();
//设置null的不匹配转换
om.setSerializationInclusion(Inclusion.NON_NULL);

写操作,写到流里面:

//回写cookie,基于流的形式
StringWriter sw = new StringWriter();
om.writeValue(StringWriter, Class);

读操作:

Class clszz= om.readValue(String, Class.class);


关键字:json 转换

© copyright 粤ICP备16108162号-1