博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用jackson来进行数组格式的json字符串转换成List。
阅读量:5084 次
发布时间:2019-06-13

本文共 5477 字,大约阅读时间需要 18 分钟。

有一个字符串如下。如下,也是通过jackson把list转换成的json字符串,我想把它转过来,看网上的内容都不尽人如意,都是片断的内容。估计只有写的知道怎么使用,所以就直接看了jackson的官网,知道了使用方法。

用的类主要是

import org.codehaus.jackson.type.TypeReference;import org.codehaus.jackson.map.ObjectMapper;

要转的字符串如下:

[{"id":"36CD0224C1ED25F5E0538A3B0B7A8190","catgId":null,"matcAmont":50000,"lendPoolId":"36CD0224C1C225F5E0538A3B0B7A8190","balance":50000,"matchFlag":1,"isLock":1,"createTime":1467576000000,"userId":0,"mltCustLendPool":{"id":"36CD0224C1C225F5E0538A3B0B7A8190","userId":157020,"state":1,"isLock":1,"balance":50000,"curTotaAmt":50000,"syncDate":1467561600000,"inviFlag":2,"investTime":1467595065000,"deadline":1499131065000,"billDate":1467681465000,"billDay":5,"productId":"6","productName":"xxx","investAmt":50000,"prodOrderId":38662,"userTelephone":"15922166933","userName":"张三","idcardNum":"1111111111111","projectNum":null,"matchModelCode":null,"creditProduct":{"id":"290AA19B1134838EE053A716C0769130","swldid":"6","prodName":"xxx","prodType":null,"prodRate":0.13,"synFeeRate":null,"payCapitalType":null,"freezeTime":12,"prodCode":6,"prodCatgory":1,"unit":1},"subject":null},"orderFlag":null,"subject":0,"projectNum":null,"matchModeCode":"1"},{"id":"36DA5B50E54E2790E0538A3B0B7A2261","catgId":null,"matcAmont":50000,"lendPoolId":"36CD0224C1C225F5E0538A3B0B7A8190","balance":50000,"matchFlag":1,"isLock":1,"createTime":1467748800000,"userId":0,"mltCustLendPool":{"id":"36CD0224C1C225F5E0538A3B0B7A8190","userId":157020,"state":1,"isLock":1,"balance":50000,"curTotaAmt":50000,"syncDate":1467561600000,"inviFlag":2,"investTime":1467595065000,"deadline":1499131065000,"billDate":1467681465000,"billDay":5,"productId":"6","productName":"xxx","investAmt":50000,"prodOrderId":38662,"userTelephone":"15922166933","userName":"王dd","idcardNum":"2222222222222","projectNum":null,"matchModelCode":null,"creditProduct":{"id":"290AA19B1134838EE053A716C0769130","swldid":"6","prodName":"dfdfd","prodType":null,"prodRate":0.13,"synFeeRate":null,"payCapitalType":null,"freezeTime":12,"prodCode":6,"prodCatgory":1,"unit":1},"subject":null},"orderFlag":null,"subject":0,"projectNum":null,"matchModeCode":"1"}]

主要代码如下:

ObjectMapper mapper = new ObjectMapper();        List
lendReco = mapper.readValue(listStr,new TypeReference
>() { }); System.out.println(lendReco.get(0).getId());

这样就可以把json字符串转换成想要的List.

注意readValue()方法里的new TypeReference<List<MltWaitLendReco>>() { }写法很关键,你写成List.class是不行的,

还有一点,ObjectMapper类在com.fasterxml.jackson开头的包里也有,这里用的是org.codehaus.jackson

 这里还有一个以前用的,对象和Json互转的工具类

import org.codehaus.jackson.JsonGenerationException;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.JsonParser;import org.codehaus.jackson.map.DeserializationConfig;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;import org.codehaus.jackson.type.TypeReference;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;public class JsonUtils {    /**     * Logger for this class     */    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);    private final static ObjectMapper objectMapper = new ObjectMapper();    static {        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);        objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);        objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);    }    public static String encode(Object obj) {        try {            return objectMapper.writeValueAsString(obj);        } catch (JsonGenerationException e) {            logger.error("encode(Object)", e); //$NON-NLS-1$        } catch (JsonMappingException e) {            logger.error("encode(Object)", e); //$NON-NLS-1$        } catch (IOException e) {            logger.error("encode(Object)", e); //$NON-NLS-1$        }        return null;    }    /**     * 将json string反序列化成对象     *     * @param json     * @param valueType     * @return     */    public static 
T decode(String json, Class
valueType) { try { return objectMapper.readValue(json, valueType); } catch (JsonParseException e) { logger.error("decode(String, Class
)", e); } catch (JsonMappingException e) { logger.error("decode(String, Class
)", e); } catch (IOException e) { logger.error("decode(String, Class
)", e); } return null; } /** * 将json array反序列化为对象 * * @param json * @param jsonTypeReference * @return */ public static
T decode(String json, TypeReference
jsonTypeReference) { try { return (T) objectMapper.readValue(json, jsonTypeReference); } catch (JsonParseException e) { logger.error("decode(String, JsonTypeReference
)", e); } catch (JsonMappingException e) { logger.error("decode(String, JsonTypeReference
)", e); } catch (IOException e) { logger.error("decode(String, JsonTypeReference
)", e); } return null; }}

 

  插播个广告 

 

 

转载于:https://www.cnblogs.com/dupang/p/5673097.html

你可能感兴趣的文章
排球积分程序(三)——模型类的设计
查看>>
python numpy sum函数用法
查看>>
php变量什么情况下加大括号{}
查看>>
linux程序设计---序
查看>>
【字符串入门专题1】hdu3613 【一个悲伤的exkmp】
查看>>
C# Linq获取两个List或数组的差集交集
查看>>
HDU 4635 Strongly connected
查看>>
ASP.NET/C#获取文章中图片的地址
查看>>
Spring MVC 入门(二)
查看>>
格式化输出数字和时间
查看>>
页面中公用的全选按钮,单选按钮组件的编写
查看>>
java笔记--用ThreadLocal管理线程,Callable<V>接口实现有返回值的线程
查看>>
BZOJ 1047 HAOI2007 理想的正方形 单调队列
查看>>
各种语言推断是否是手机设备
查看>>
这个看起来有点简单!--------实验吧
查看>>
PHP count down
查看>>
JVM参数调优:Eclipse启动实践
查看>>
(旧笔记搬家)struts.xml中单独页面跳转的配置
查看>>
不定期周末福利:数据结构与算法学习书单
查看>>
strlen函数
查看>>