rabbitMq消息接收转换对象,Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62解决
Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62
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":""}}}
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字符串格式
String msgNew = msg;
if(msg.startsWith("\"") && msg.endsWith("\"")){
msgNew = msg.substring(1,msg.length() - 1).replace("\\","");
}
rabbitMq消息接收转换对象,Json解析字符串报错syntax error, expect {, actual string, pos 0, fastjson-version 1.2.62解决的更多相关文章
- C# string.Format json格式字符串报错”输入字符串的格式不正确“
当我们在string.Format中传入Json字符串时,会报”输入字符串的格式不正确“,这是因为json的"{"符号的问题,最开始我是想着用转义一下"{",但 ...
- JSON parse error: syntax error, expect {, actual error, pos 0, fastjson-version 1.2.58; nested exception is com.alibaba.fastjson.JSONExcetion: syntax error, except {, actual error, pos ...
这个报错信息告诉你,你提交的参数需要是json类型.所以,POST请求携带的数据需要序列化一下json.dumps(data).
- redis通过json方案存取对象com.alibaba.fastjson.JSONException: syntax error, expect
问题描述: redis基于json方案存取对象时报错:com.alibaba.fastjson.JSONException: syntax error, expect com.alibaba.fast ...
- ESL python调用C模块时传递unicode字符串报错问题解决
在是用freeswitch时利用ESL的python调用时传递字符串报错 TypeError: in method 'ESLconnection_api', argument 2 of type 'c ...
- iOS解析JSON字符串报错Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."
将服务器返回的JSON string转化成字典时报错: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence ...
- artTemplate--使用artTemplate时,由于json对象属性有数字命名格式 导致调用报错 syntax error
案例 今天在使用artTemplate做开发时,遇到一个比较奇葩的问题,就是使用json对象去获取值得时候,报如下错误: Template Error <temp> function an ...
- iOS中空字符串报错
*参考: http://www.ithao123.cn/content-8030945.html *参考: http://www.cnblogs.com/ziyi--caolu/p/4825633.h ...
- rabbitmq - 消息接收,解析xml格式数据时异常:ERROR not well-formed (invalid token): line 4, column 46
ERROR alsv odoo.addons.cus_alsv.utils.alsv_about_mq.get_data_from_mq: parse_xml_data_from_mq: not we ...
- python 字符串报错问题
http://jingyan.baidu.com/article/25648fc1a96dd49191fd00c0.html 解决'ascii' codec can't encode characte ...
- 在 VSCode 中 Angular 的字符串报错的问题
使用 Angular 时,报错 [tslint] " should be ' 报错原因是因为 ESLint 的严格模式,限制了使用 ' ,甚至多一个空格都会报错. 我们只需要在 settin ...
随机推荐
- 国内唯一!阿里云容器服务进入 Forrester 领导者象限
简介:近日,国际权威咨询机构 Forrester 发布< The Forrester WaveTM: Public Cloud Container Platforms, Q1 2022 > ...
- SpringBoot Admin2.0 集成 Java 诊断神器 Arthas 实践
简介: 项目最初使用 Arthas 主要有两个目的: 1. 通过 arthas 解决实现测试环境.性能测试环境以及生产环境性能问题分析工具的问题. 2. 通过使用 jad.mc.redefine 功能 ...
- CPU Burst有副作用吗?让数学来回答!| 龙蜥技术
简介: 使用CPU Burst的副作用是什么?是否有不适用的场景呢?戳我给你答案~ 编者按:CPU Burst 特性已合入 Linux 5.14,Anolis OS 8.2.Alibaba Clou ...
- [GPT] swoole的协程和golang的协程有什么区别,哪个更好
Swoole 的协程和 Golang(Go 语言)的协程(Goroutine)在概念上都是为了实现轻量级的并发编程,但它们在具体实现.使用方式和性能特点上有所不同: 实现原理: Golang 协程(G ...
- [MySQL] 原生全文检索 fulltext 的简单应用
在目标字段上添加全文检索:alter table 表名 add fulltext(字段) with parser ngram 查询语句:select * from xxx where match(字段 ...
- WPF 设置 IncludePackageReferencesDuringMarkupCompilation 属性导致分析器不工作
本文记录在 WPF 项目里面设置 IncludePackageReferencesDuringMarkupCompilation 属性为 False 导致了项目所安装的分析器不能符合预期工作 设置 I ...
- Go语言基础(一)
一.Go语言简介 Go即Golang,是Google公司2009年11月正式对外公开的一门语言 Go是静态强类型语言,是区别于解析型语言的编译型语言. 解析型语言--源代码是先翻译为中间代码,然后解析 ...
- 探索 DTD 在 XML 中的作用及解析:深入理解文档类型定义
DTD 是文档类型定义(Document Type Definition)的缩写.DTD 定义了 XML 文档的结构以及合法的元素和属性. 为什么使用 DTD 通过使用 DTD,独立的团体可以就数据交 ...
- Node.js 万字教程
0. 基础概念 Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,使用了一个事件驱动.非阻塞式 I/O 模型,让 JavaScript 运行在服务端的开发平台. ...
- 5G 系统流程系列:AF 的 Traffic Routing Control 以及 UP 路径管理增强
目录 文章目录 目录 前言 引用 术语 AF 接入 5GC 的 3 种方式 AF Request 及其 Service Information 流量描述(Traffic Description) N6 ...