admin管理员组文章数量:1487745
rabbitMq消息接收转换对象,Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson
代码语言:javascript代码运行次数:0运行复制Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
代码语言:javascript代码运行次数:0运行复制syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
代码语言:javascript代码运行次数:0运行复制syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
以上的报错都是Json字符串格式错误,比如缺少{},比如两头多了”“,或者转义字符\",比如在映射的对象错误,本来是List,但是写成了String,或者是单个对象,而不是List,都会报错。
1.在本地运行main方法排查解决
2.如果是多了”“,或者是内部多了转义字符\",则使用简单的替换方法。
比如完整的Json字符串格式如下:
{"err_no":"0","err_tips":"success","data":{"refund_list":[{"refund_id":"ots7240038790928135","out_refund_no":"29160417157000010","refund_total_amount":1090,"refund_status":"PROCESSING","refund_at":0,"message":"","order_id":"ots72396406088135","item_order_detail":[{"item_order_id":"ots723969678768135","refund_amount":1090}],"merchant_audit_detail":"{\"deny_message\":\"\",\"need_refund_audit\":1,\"refund_audit_deadline\":16859682,\"audit_status\":\"TOAUDIT\"}","create_at":1685701000,"refund_source":2}],"refund_id":"ots72400387928135","out_refund_no":"2916041715168561000010","refund_total_amount":1090,"refund_status":"PROCESSING","refund_at":0,"message":"","order_id":"ots7239640656088135","item_order_detail":[{"item_order_id":"ots72396406578768135","refund_amount":1090}],"merchant_audit_detail":{"need_refund_audit":1,"refund_audit_deadline":168596282,"audit_status":"TOAUDIT","deny_message":""}}}
代码语言:javascript代码运行次数:0运行复制package com.example.core.mydemo.json2;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.reflect.TypeToken;
public class JSONTest {
public static void main(String[] args) {
String msg = "\"orderNo\":\"50133310603299\",\"riskAccidentStatus\":\"1\",\"operator\":\"admin\"";
//Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
//缺少的是{}
try {
MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO = GsonUtils.convert(msg, new TypeToken<MQReceiveCLevelRiskControlDTO>() {});
System.out.println("gson1=" + GsonUtils.toJson(cLevelRiskControlDTO));
}catch (Exception e) {
MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO = GsonUtils.convert("{"+msg+"}", new TypeToken<MQReceiveCLevelRiskControlDTO>() {});
System.out.println("gson2=" + GsonUtils.toJson(cLevelRiskControlDTO));
}
//syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
// MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO2 = JSON.parseObject(msg, MQReceiveCLevelRiskControlDTO.class);
// System.out.println("gson2=" + GsonUtils.toJson(cLevelRiskControlDTO2));
//syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
// MQReceiveCLevelRiskControlDTO cLevelRiskControlDTO3 = JSONObject.parseObject(msg, MQReceiveCLevelRiskControlDTO.class);
// System.out.println("gson3=" + GsonUtils.toJson(cLevelRiskControlDTO3));
}
}
解决方法:过滤json字符串格式
代码语言:javascript代码运行次数:0运行复制String msgNew = msg;
if(msg.startsWith("\"") && msg.endsWith("\"")){
msgNew = msg.substring(1,msg.length() - 1).replace("\\","");
}
代码语言:javascript代码运行次数:0运行复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-06-05,如有侵权请联系 cloudcommunity@tencent 删除expectstringsyntax对象字符串本文标签:
版权声明:本文标题:rabbitMq消息接收转换对象,Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/shuma/1754978263a3181994.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论