javascript 处理 JSON 中的 html 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6968804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling html code in JSON
提问by Franz Payer
I am writing code that will use AJAX requests to get some HTML code from the server to embed, but I cannot seem to get the JSON format correct. Can anyone tell me what I am doing wrong?
我正在编写将使用 AJAX 请求从服务器获取一些 HTML 代码以嵌入的代码,但我似乎无法正确获取 JSON 格式。谁能告诉我我做错了什么?
{"response":
[
{"code": "<div id=\”sponsors\” class=\”infoBox\” > <div class=\”title\”>THANK YOU 2011 SPONSORS!</div> <div class=\”section\”> <div class=\”party\”><a href=\”http://www.ChickRussell.com\”>Chick Russell Communications</a></div> <div class=\”cityState\”>Pasadena, California</div> <div class=\”description\”> Chick Russell Communications is a story-driven creative development company, not a design-driven company. It's one of our main distinguishing features. It doesn't matter how great it looks if it doesn't create the desired effect. <a href=\”/vendors/info/17280\”>more...</a> </div> <div class=\”web\”><a href=\”http://www.ChickRussell.com\”>www.ChickRussell.com</a></div> </div> </div>"
}
]
}
When I try to run JSON.parse()
on it, I get a syntax error
当我尝试运行JSON.parse()
它时,出现语法错误
Here is the code I am using to read the JSON:
这是我用来读取 JSON 的代码:
<script language="JavaScript" type="text/javascript">
var newxhr = new XMLHttpRequest()
newxhr.onreadystatechange = getResponse
function getResponse(){
if(newxhr.readyState == 4) {
var response = newxhr.responseText;
console.log(response)
response = JSON.parse(response)
newxhr.close;
}
}
var url = "http://*.*.net/test.json";
newxhr.open("GET", url, true);
newxhr.send(null)
</script>
回答by Quentin
”
and "
are different characters. You need strightquotes, not curly ones. (Tell your browser to zoom in if you can't see the difference … and don't use an editor (e.g. many word processors) that converts to curly quotes automatically, use a proper text editor (I'm fond of ActiveState Komodo)).
”
并且"
是不同的字符。您需要直引号,而不是卷曲引号。(如果您看不到差异,请告诉您的浏览器放大……并且不要使用自动转换为卷曲引号的编辑器(例如许多文字处理器),使用适当的文本编辑器(我喜欢 ActiveState Komodo) )。
Old answer (before the JSON in the question was revised):
旧答案(在问题中的 JSON 被修改之前):
The first thing you are probably doing wrong, is trying to build JSON by hand. Use a library instead.
您可能做错的第一件事是尝试手动构建 JSON。改用图书馆。
The second thing you are doing wrong is HTML encoding (badly) your quote marks. To escape a "
inside a JSON string you represent it as \"
.
您做错的第二件事是 HTML 编码(严重)您的引号。要"
在 JSON 字符串中转义 a ,您将其表示为\"
.
The third thing (but it could be just that you are giving a poor example) is bothering to use an array for a single object.
第三件事(但可能只是你举了一个糟糕的例子)是麻烦地为单个对象使用数组。
When I try to run JSON.parse() on it, I get a syntax error
当我尝试在其上运行 JSON.parse() 时,出现语法错误
Despite the problems with it, what you have is valid JSON, so that should not happen. You appear to have reduced your test case so far that the problem you are having doesn't appear in it.
尽管存在问题,但您拥有的是有效的 JSON,因此不应发生这种情况。到目前为止,您似乎已经减少了测试用例,因此您遇到的问题没有出现在其中。
回答by Frederic
It's the double quotes. You used ” (Unicode: U+8221) instead of " (Unicode: U+34). Although they look very similar, they are two different chars.
是双引号。您使用了“(Unicode:U+8221)而不是“(Unicode:U+34)。虽然它们看起来非常相似,但它们是两个不同的字符。
The code below is valid:
以下代码有效:
{
"response": [
{
"code": "<div id=\"sponsors\" class=\"infoBox\" > <div class=\"title\">THANK YOU 2011 SPONSORS!</div> <div class=\"section\"> <div class=\"party\"><a href=\"http://www.ChickRussell.com\">Chick Russell Communications</a></div> <div class=\"cityState\">Pasadena, California</div> <div class=\"description\"> Chick Russell Communications is a story-driven creative development company, not a design-driven company. It's one of our main distinguishing features. It doesn't matter how great it looks if it doesn't create the desired effect. <a href=\"/vendors/info/17280\">more...</a> </div> <div class=\"web\"><a href=\"http://www.ChickRussell.com\">www.ChickRussell.com</a></div> </div> </div>"
}
]
}
You can always check your JSON code with http://jsonlint.com/- it's a great tool.
您可以随时使用http://jsonlint.com/检查您的 JSON 代码- 这是一个很棒的工具。
回答by Ilia Choly
it seems to work for me: http://jsfiddle.net/6PV4M/1/
它似乎对我有用:http: //jsfiddle.net/6PV4M/1/
however, i did escape the single quotes.
但是,我确实逃脱了单引号。
var str = '{"response":[{"code": "<div id=\”sponsors\” class=\”infoBox\” > <div class=\”title\”>THANK YOU 2011 SPONSORS!</div> <div class=\”section\”> <div class=\”party\”><a href=\”http://www.ChickRussell.com\”>Chick Russell Communications</a></div> <div class=\”cityState\”>Pasadena, California</div> <div class=\”description\”> Chick Russell Communications is a story-driven creative development company, not a design-driven company. It\'s one of our main distinguishing features. It doesn\'t matter how great it looks if it doesn\'t create the desired effect. <a href=\”/vendors/info/17280\”>more...</a> </div> <div class=\”web\”><a href=\”http://www.ChickRussell.com\”>www.ChickRussell.com</a></div> </div> </div>"} ]}';
var obj = JSON.decode(str);
alert(obj["response"][0]["code"]);