我如何准备一个字符串进行解析

问题描述

我需要解析

'[{"message":"Argument \"absoluteDateTimeFilter\" has invalid value {startDate: \"2021-03-01\",endDate: \"2021-03-09\",groupingLevel: undefined}.\nIn field \"groupingLevel\": Expected type \"PeriodLevelId\",found undefined.","locations":[{"line":1,"column":70}]}]'
  • 字符串类型

分成两个键值对 - 'message' 和 'location'。但我得到 SyntaxError 引用 \"absoluteDateTimeFilter\"

我尝试了什么

当我硬编码空格而不是 \"\" 时,它仍然收到错误,但现在它链接.\n

这个问题有灵活的解决方案吗?

谢谢!

解决方法

你可以试试这个。它对你有用。

const data = [{"message":"Argument \"absoluteDateTimeFilter\" has invalid value {startDate: \"2021-03-01\",endDate: \"2021-03-09\",groupingLevel: undefined}.\nIn field \"groupingLevel\": Expected type \"PeriodLevelId\",found undefined.","locations":[{"line":1,"column":70}]}];

let stringfyData=JSON.stringify(data);
let strReplaced = stringfyData.replaceAll(String.fromCharCode(92,34),"'");
 let parseArray=JSON.parse(strReplaced);
 console.log("Message::",parseArray[0].message);
console.log("locations::",parseArray[0].locations);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...