赞
踩
在使用fastjson的JSONObject.parseObject()进行对象转换时,报错如下:
// 出错代码
final String data = respBody.get("data").toString();
UserVO vo = JSONObject.parseObject(data, UserVO.class);
// 报错
com.alibaba.fastjson.JSONException: not match : - =, info : pos 8, line 1, column 9{staffId=xiaowang, loginId=xiaowang, staffName=小王, sex=10000012, phone=18183253005, email=testmail@qq.com, idCard=51234567899093, birth=2002-12-11, graduatSchool=null, education=10000021, employDt=2021-09-16, policitalStatus=10000053, validity=10000001, remark=, roleId=root, roleName=超级管理员1, positionId=P10001, positionName=董事长, deptId=O10002, deptName=董事会}
at com.alibaba.fastjson.parser.JSONLexerBase.nextTokenWithChar(JSONLexerBase.java:396)
at com.alibaba.fastjson.parser.JSONLexerBase.nextTokenWithColon(JSONLexerBase.java:506)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1275)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:892)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseRest(JavaBeanDeserializer.java:1613)
at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_UserVO.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:296)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:694)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:395)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:299)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:572)
先将对象转化成JSONString在进行对象转换,不能将字符串直接toString()不能将字符串JSON转义,在进行对象转换是就会报错
// 先转为json string,再进行对象转换
final String userStr = JSON.toJSONString(respBody.get("data"));
final UserVO vo = JSON.parseObject(userStr, UserVO.class);
如果是数组,使用如下:
// 先转为json array string,再调用JSON.parseArray转换成对应对象list
String userArrStr = JSONArray.toJSONString(respBody.get("data"));
List<UserVO> vo= JSON.parseArray(userArrStr, UserVO.class);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。