解析json去掉反斜杠_解析json时处理反斜杠

Here is a portion of a larger JSON string that I attempting to call JSON.parse on. I am getting the infamous 'invalid character' error because (I believe) of the backslash parentheses

All online parsers I have tried it on works fine but in my javascript code the JSON.parse method fails.

I have attempted to clean the string like this and other ways but I have been unable to get it to parse.

var cleanData = data.replace(/\\"/, /\\\\/);

below is the partial JSON file. When I remove it from the JSON string the JSON.parse works so I think I have this isolated to just this. What type of general purpose clean method would work to get this thing to parse? Thansk

'{"Properties" : {

"GenerationId" : 9223372036854776000,

"indexSystem" : "",

"ExecutionTimeMs" : 109,

"QueryModification" : "path:\"https://mysite.sharepoint.com/sites/Test\" (IsDocument:\"True\" OR contentclass:\"STS_ListItem\") ContentTypeId:0x0120D5200098CBB075E51C8C4398ECCB4B4928912D*",

"RenderTemplateId" : "~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js",

"StartRecord" : 0,

"piPageImpressionBlockType" : 2

}}

how?

解决方案

The problem is that your backslash is getting swallowed as an escape character in the string:

'\"' === '"' // true

You actually need to escape the backslashes, so that the JSON parser sees them. Here's another example:

var unencoded = 'string with "quotes"';

'"string with \"quotes\""' === JSON.stringify(unencoded); // false

'"string with \\"quotes\\""' === JSON.stringify(unencoded); // true

However, where the escaping should be done depends on how the JSON is being made available to the JavaScript. If the JSON is embedded in the page by a server-side script, then there's no need to use JSON.parse, as valid JSON is valid JavaScript:

// if JsonData is valid JSON, it's also a valid JavaScript object

var data = ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值