我有一个带有html标记的json对象和许多转义字符,如下所示,
[{
"status": "PASS",
"server_name": "sk-vbox",
"start_time": "20150528 11:49:49 Hrs",
"summay_html": "<a href=\"#sk-vboxSgTcInitiatorStartTest\">Summary</a>",
"end_time": "20150528 11:49:51 Hrs",
"log_html": "<a class=\"log\" href=\"log.html#s1-s2-s1\">Logs</a>",
"suite": "Sg Tc Initiator Start Test",
"error_msg": "-"
}]
我正在尝试使用JSON.parse解析它,如下所示:
var a = JSON.parse('[{"status": "PASS", "server_name": "sk-vbox", "start_time": "20150528 11:49:49 Hrs", "summay_html": "<a href=\"#sk-vboxSgTcInitiatorStartTest\">Summary</a>", "end_time": "20150528 11:49:51 Hrs", "log_html": "<a class=\"log\" href=\"log.html#s1-s2-s1\">Logs</a>", "suite": "Sg Tc Initiator Start Test", "error_msg": "-"}]')
它抛出错误,
JSON.parse: expected ',' or '}' after property value in object at line 1 column 111 of the JSON data
我读过here,可以通过单个反斜杠来逃避双引号,但它不起作用。
答案 0 :(得分:1)
您需要分隔字符串。想一想:
var a = JSON.parse("[{"status
那里有什么问题?看看引用......
您可以将整个字符串包装在'
中(如果保证不包含任何'
本身):
var a = JSON.parse('[{"status": "PASS", "server_name": "sk-vbox", "start_time": "20150528 11:49:49 Hrs", "summay_html": "<a href=\"#sk-vboxSgTcInitiatorStartTest\">Summary</a>", "end_time": "20150528 11:49:51 Hrs", "log_html": "<a class=\"log\" href=\"log.html#s1-s2-s1\">Logs</a>", "suite": "Sg Tc Initiator Start Test", "error_msg": "-"}]')
或者您可以分隔其中的所有"
(以及对已经分隔的HTML进行双重分隔):
var a = JSON.parse("[{\"status\": \"PASS\", \"server_name\": \"sk-vbox\", \"start_time\": \"20150528 11:49:49 Hrs\", \"summay_html\": \"<a href=\\\"#sk-vboxSgTcInitiatorStartTest\\\">Summary</a>\", \"end_time\": \"20150528 11:49:51 Hrs\", \"log_html\": \"<a class=\\\"log\\\" href=\\\"log.html#s1-s2-s1\\\">Logs</a>\", \"suite\": \"Sg Tc Initiator Start Test\", \"error_msg\": \"-\"}]")