当前位置: 首页 > news >正文

Json 转义符号处理(Mongo changeStream op log)

使用mongo-kafka组件订阅mongo的changeStream得到 一个带有很多转义符号的json字符串

"{\"_id\": {\"_data\": \"8267D0F733000001502B022C0100296E5A1004366730C56F7E41A790BDA4CF23259A4F46645F6964006467B91713A024A00E32CDF6800004\"}, \"operationType\": \"update\", \"clusterTime\": {\"$timestamp\": {\"t\": 1741748019, \"i\": 336}}, \"fullDocument\": {\"_id\": \"67b91713a024a00e32cdf680\", \"platformUID\": \"1863032388537794561\", \"createdAt\": \"2025-02-22T00:15:15.013Z\", \"meta\": {\"handle\": \"Petrada_he\", \"profile_image_url\": \"https://pbs.twimg.com/profile_images/1892687444467662849/PQ6qRoUj_normal.png\", \"description\": \"Cofounder @TheBigWhale_ I Journaliste I Auteur de \\\"Bitcoin Cryptos, l'enjeu du siècle\\\"\", \"followers\": 2, \"following\": 268, \"tweetCount\": 307}, \"monitorFollowingTeam\": [], \"platform\": \"twitter\", \"updatedAt\": \"2025-03-12T02:53:39.129Z\"}, \"ns\": {\"db\": \"bifrost\", \"coll\": \"socialaccounts\"}, \"documentKey\": {\"_id\": \"67b91713a024a00e32cdf680\"}, \"updateDescription\": {\"updatedFields\": {\"meta\": {\"handle\": \"Petrada_he\", \"profile_image_url\": \"https://pbs.twimg.com/profile_images/1892687444467662849/PQ6qRoUj_normal.png\", \"description\": \"Cofounder @TheBigWhale_ I Journaliste I Auteur de \\\"Bitcoin Cryptos, l'enjeu du siècle\\\"\", \"followers\": 2, \"following\": 268, \"tweetCount\": 307}, \"updatedAt\": \"2025-03-12T02:53:39.129Z\"}, \"removedFields\": [], \"truncatedArrays\": []}}"

这段json直接使用 fastjson直接解析会报Cast异常 :

JSONObject jsonObject = JSONObject.parseObject(rawString);

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.alibaba.fastjson.JSONObject

使用replace方法去掉转义符号会误杀 value中的转义符号:

JSONObject jsonObject = JSONObject.parseObject(rawString.replace("\\\"", "\""))

com.alibaba.fastjson.JSONException: not close json text, token : error

既想去掉key 两边的转义符号,又不误杀value中包含的转义符号,需要使用正则:

 String output = rawString.replaceAll("(?<!\\\\)\\\\\"", "\"");

另一条数据 有特殊符号,正则处理不了

"{\"_id\": {\"_data\": \"8267D127240000012D2B022C0100296E5A1004366730C56F7E41A790BDA4CF23259A4F46645F6964006466EAB2B5000A4DD13F9615D20004\"}, \"operationType\": \"update\", \"clusterTime\": {\"$timestamp\": {\"t\": 1741760292, \"i\": 301}}, \"fullDocument\": {\"_id\": \"66eab2b5000a4dd13f9615d2\", \"platformUID\": \"1807346982681497600\", \"createdAt\": \"2024-09-18T11:00:05.753Z\", \"meta\": {\"handle\": \"cyberzone5758\", \"profile_image_url\": \"https://pbs.twimg.com/profile_images/1832300244513480706/deeABfbw_normal.jpg\", \"description\": \"²\\u207f\\u1d48 \\u1d43\\u1d9c\\u1d9c\\u1d52\\u1d58\\u207f\\u1d57 ⁻ \\u1d43\\u1d4f\\u1d58\\u207f \\u1d4f\\u1d49\\u1d48\\u1d58\\u1d43\\n\\n/Saya ini orang yang gampang, tapi jangan suka gampangin saya\\\\\", \"followers\": 87, \"following\": 233, \"tweetCount\": 1553}, \"monitorFollowingTeam\": [], \"platform\": \"twitter\", \"updatedAt\": \"2025-03-12T06:18:12.556Z\"}, \"ns\": {\"db\": \"bifrost\", \"coll\": \"socialaccounts\"}, \"documentKey\": {\"_id\": \"66eab2b5000a4dd13f9615d2\"}, \"updateDescription\": {\"updatedFields\": {\"meta\": {\"handle\": \"cyberzone5758\", \"profile_image_url\": \"https://pbs.twimg.com/profile_images/1832300244513480706/deeABfbw_normal.jpg\", \"description\": \"²\\u207f\\u1d48 \\u1d43\\u1d9c\\u1d9c\\u1d52\\u1d58\\u207f\\u1d57 ⁻ \\u1d43\\u1d4f\\u1d58\\u207f \\u1d4f\\u1d49\\u1d48\\u1d58\\u1d43\\n\\n/Saya ini orang yang gampang, tapi jangan suka gampangin saya\\\\\", \"followers\": 87, \"following\": 233, \"tweetCount\": 1553}, \"updatedAt\": \"2025-03-12T06:18:12.556Z\"}, \"removedFields\": [], \"truncatedArrays\": []}}"

 

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String formatedJson = engine.eval(rawString).toString();

使用js,将string当作代码行执行可去除转义符号

http://www.dtcms.com/a/66481.html

相关文章:

  • SAP访问外围系统(http/https)
  • Android笔记:Android平台下SVG格式的解析与实践
  • ardunio R4 WiFi连接实战
  • 《Transformer如何进行图像分类:从新手到入门》
  • 人工智能基础知识笔记四:聚类分析
  • HOT100——二叉树篇Leetcode236. 二叉树的最近公共祖先
  • 【H2O2 | 软件开发】Axios发送Http请求
  • 队列的简单例题
  • cyberstrikelab lab2
  • 星越L_发动机舱开启及油液加注讲解
  • sqldef:一款免费的数据库变更管理工具
  • 基于llama_cpp 调用本地模型(llama)实现基本推理
  • Redis三大件 穿透、雪崩、击穿
  • 蓝桥杯进制问题秒破解法|冲击省一题单(二)
  • Spring 注解解析
  • 如何实现具备自动重连与心跳检测的WebSocket客户端
  • lambda表达式案例分析
  • Vector底层结构和源码分析(JDK1.8)
  • 【不动产登记全解析】范围、内容与不予登记的情形
  • 思维链医疗编程方法论框架(Discuss V1版)
  • MySQL 8 设置允许远程连接(Windows环境)
  • Oracle 查询数据库对象的DDL语句
  • 解锁 AI 开发的无限可能:邀请您加入 coze-sharp 开源项目
  • antd的Form表单校验的方式有几种
  • 网络空间安全(29)漏洞扫描
  • undefined和null的区别 (新手小白级教程)
  • 第5关:猴子爬山
  • 航空动力系统适航标准要点手册
  • OpenRewrite配方之import语句的顺序——org.openrewrite.java.OrderImports
  • transformer bert 多头自注意力