前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rabbitMq消息接收转换对象,Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62解决

rabbitMq消息接收转换对象,Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62解决

作者头像
oktokeep
发布2024-10-09 10:54:31
960
发布2024-10-09 10:54:31
举报
文章被收录于专栏:第三方工具
代码语言:javascript
复制
Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $

代码语言:javascript
复制
syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
代码语言:javascript
复制
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
复制
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
复制
String msgNew = msg;
if(msg.startsWith("\"") && msg.endsWith("\"")){
    msgNew = msg.substring(1,msg.length() - 1).replace("\\","");
}
代码语言:javascript
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档