问题描述
我需要解析
'[{"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);