在使用FastJSON,使用如下代码:
List<SignEndorseDTO> endorseComs = JSONObject.parseArray(draftDetail.getString("endorse_coms"), SignEndorseDTO.class);
则会出现如下错误:
Exception in thread "main" com.alibaba.fastjson.JSONException:not match : - =, info : pos 13, line 1, column 14[ {endorseDate=2021-12-24, endorseName=临xxx有限公司, endorseeName=费xxx有限公司, isTransfer=aa, title=aaa}]
为什么出现这个错误呢?
原始是FastJSON的getString()引起的。getString()实际是object.toString,导致格式无法解析导致转换错误
{endorseDate=2021-12-24, endorseName=临xxx有限公司, endorseeName=费xxx有限公司, isTransfer=可aa, title=转bb}
而正确的转换格式:
{"id":"66","firmware_id":"15","version":"V20190617_170943","file_hash":"c9782aeff870c229f4a9d3efbaa11fa7","file_size":"511020","description":"测试","product_id":"8"}
所以,正确的使用方式:
JSONArray detailJSONArray = draftDetail.getJSONArray("endorse_coms");
List<SignEndorseDTO> endorseComs = JSONObject.parseArray(JSONObject.toJSONString(detailJSONArray), SignEndorseDTO.class);
至于object.toString为什么会转换成
{endorseDate=2021-12-24, endorseName=临xxx有限公司, endorseeName=费xxx有限公司, isTransfer=可aa, title=转bb}
可以看一下对象重写后的toString
讲清楚为什么,才会明白为什么要这么做。
若有错误的地方,请与指正,欢迎讨论