使用HTML标记和引号解析JSON

时间:2018-11-30 17:23:12

标签: javascript json

我有一个字符串输入到一个看起来像这样的函数中:

rpart.plot(fit, yesno=2, box.palette = 0, extra=100, under = TRUE, split.fun = split.fun, split.font=1, split.family="Arial Unicode MS", family="Arial Unicode MS")

我正在尝试使用以下方式解析此字符串:

properyCharacteristics  "{\"Key\":12345,\"values\":[\"<a target=\"_blank\" href=\"https://www.doamin.ca/residents/assessment/understand-assessment/assessment-glossary/index.htm#valuation_neighbourhood\">Valuation Neighbourhood Number</a>: <a target=\"_blank\" href=\"{{GIS_TAX_WEB_VIEWER}}?resval=1622\" class=\"map-overlay-link\">1622</a>\"]}"

但不断收到“无效字符”错误。

是\“无效吗?我尝试使用:

var data = JSON.parse(input);

删除这些字符,但我仍然收到“无效字符”错误。

2 个答案:

答案 0 :(得分:0)

HTML字符串内的引号 必须转义为\\\"。简单的\"-成为JSON字符串本身的引号,然后您就有了

{"Key":12345,"values":["<a target="_blank"
href="https://www.doamin.c....essment-glossary/index.htm#valuation_neighbourhood">
Valuation Neighbourhood Number</a>: <a target="_blank"
href="{{GIS_TAX_WEB_VIEWER}}?resval=1622"
class="map-overlay-link">1622</a>"]}

所以大量随机放置的引号使整个内容无效。

内部转义了两次,它突然起作用了:

var text="{\"Key\":12345,\"values\":[\"<a target=\\\"_blank\\\" href=\\\"https://www.doamin.ca/residents/assessment/understand-assessment/assessment-glossary/index.htm#valuation_neighbourhood\\\">Valuation Neighbourhood Number</a>: <a target=\\\"_blank\\\" href=\\\"{{GIS_TAX_WEB_VIEWER}}?resval=1622\\\" class=\\\"map-overlay-link\\\">1622</a>\"]}";
console.log(JSON.parse(text));

答案 1 :(得分:0)

尝试一下。

properyCharacteristics = "{\"Key\":12345,\"values\":[\"<a target=\"_blank\" href=\"https://www.doamin.ca/residents/assessment/understand-assessment/assessment-glossary/index.htm#valuation_neighbourhood\">Valuation Neighbourhood Number</a>: <a target=\"_blank\" href=\"{{GIS_TAX_WEB_VIEWER}}?resval=1622\" class=\"map-overlay-link\">1622</a>\"]}";
var data = JSON.parse(JSON.stringify(properyCharacteristics));
console.log(data);