Mar*_*Lin 4 html javascript json
我有一个这样的字符串:
{"Restriction":"<wbr><a href=\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\"
target=\"_blank\"><span style=\"color: rgb(0, 0, 205);\">more info</span></a></wbr>"}
Run Code Online (Sandbox Code Playgroud)
但我无法用 JSON.parse 解析它。我的代码如下所示:
var s = '{"Restriction":"<wbr><a href=\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\" target=\"_blank\"><span style=\"color: rgb(0, 0, 205);\">more info</span></a></wbr>"}';
var obj = JSON.parse(s);
Run Code Online (Sandbox Code Playgroud)
我得到了错误:
未捕获的语法错误:意外的标记。
我的猜测是 ?\"? 出了点问题,但我无法更改字符串,因为我是通过调用远程 API 获得的。这是我的代码:
// We need this to build our post string
var querystring = require('querystring');
var http = require('http');
var fs = require('fs');
function PostCode(codestring) {
// An object of options to indicate where to post to
var post_options = {
host: 'api.domain',
port: '80',
path: '/webservice/service.asmx/method?key=123456',
method: 'GET',
headers: {
'Content-Type': 'text/plain'
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
var x = {};
console.log('Response down');
x = JSON.parse(chunk);
});
});
post_req.end();
}
PostCode();
Run Code Online (Sandbox Code Playgroud)
它不是有效的 JSON。反斜杠也应该转义。
var s = '{"Restriction":"<wbr><a href=\\"https://www.google.com.tw/#q=%E4%B8%AD%E5%9C%8B\\" target=\\"_blank\\"><span style=\\"color: rgb(0, 0, 205);\\">more info</span></a></wbr>"}';
JSON.parse(s); // correct
Run Code Online (Sandbox Code Playgroud)
我认为,您应该向 this 发布错误报告remote API。