admin管理员组文章数量:1421500
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 (ie. "path:\"\").
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:\"\" (IsDocument:\"True\" OR contentclass:\"STS_ListItem\") ContentTypeId:0x0120D5200098CBB075E51C8C4398ECCB4B4928912D*",
"RenderTemplateId" : "~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js",
"StartRecord" : 0,
"piPageImpressionBlockType" : 2
}}
how?
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 (ie. "path:\"https://mysite.sharepoint./sites/Test\").
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./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?
Share Improve this question asked Oct 21, 2014 at 22:07 ChiliYagoChiliYago 12.4k23 gold badges82 silver badges131 bronze badges 3- How does the JSON content get passed to the JavaScript? Embedded in the page by a server-side script? Ajax request? – cmbuckley Commented Oct 21, 2014 at 22:39
- Yes it is a string property on the SharePoint context object called ctx. The ctx is available to Javascript client. – ChiliYago Commented Oct 21, 2014 at 22:46
- Can you show a bit more of an example of what you're trying to do? What JavaScript client are you referring to? What other technologies are being used here? – cmbuckley Commented Oct 21, 2014 at 22:52
1 Answer
Reset to default 3The 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 = <%= JsonData %>;
本文标签: javascriptHandling backslash when parsing jsonStack Overflow
版权声明:本文标题:javascript - Handling backslash when parsing json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745352948a2654882.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论