当前位置:   article > 正文

alibaba fastjson对象转换异常com.alibaba.fastjson.JSONException: not match : - =, info : pos 8, line 1,xxx

com.alibaba.fastjson.jsonexception: not match : - =

问题:

在使用fastjson的JSONObject.parseObject()进行对象转换时,报错如下:

		// 出错代码
		final String data = respBody.get("data").toString();
        UserVO vo = JSONObject.parseObject(data, UserVO.class);
  • 1
  • 2
  • 3
// 报错
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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

解决:

先将对象转化成JSONString在进行对象转换,不能将字符串直接toString()不能将字符串JSON转义,在进行对象转换是就会报错

        // 先转为json string,再进行对象转换
        final String userStr = JSON.toJSONString(respBody.get("data"));
        final UserVO vo = JSON.parseObject(userStr, UserVO.class);
  • 1
  • 2
  • 3

如果是数组,使用如下:

		// 先转为json array string,再调用JSON.parseArray转换成对应对象list
		String userArrStr = JSONArray.toJSONString(respBody.get("data"));
		List<UserVO> vo= JSON.parseArray(userArrStr, UserVO.class);
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/923046
推荐阅读
相关标签
  

闽ICP备14008679号