[英]HTML decoding issue with quotes
I have an input hidden element, the value of which contains JSON of an HTML encoded string. 我有一个输入隐藏元素,其值包含HTML编码字符串的JSON。 Something like
就像是
{"QuestionInstruction":"<snippet lang="java"> public class Test() {\n\n}</snippet>","QuestionDescription":"it this correct?","Choices":["True","False"] }
and when I do 当我做的时候
var text = document.getElementById('inputId').value,
the " automatically gets converted to "". “”会自动转换为“”。 The value of
text
comes as - text
的价值为-
{"QuestionInstruction":"<snippet lang="java"> public class Test() {\n\n}</snippet>","QuestionDescription":"it this correct?","Choices":["True","False"] }
So, then JSON parsing fails. 因此,JSON解析失败。 :(
:(
I am using FF9. 我正在使用FF9。
I have managed to get around this by using the following function if I know to expect a value that could contain quotes when creating a JSON string/object... 如果我知道期望在创建JSON字符串/对象时可能包含引号的值,则可以通过使用以下函数来解决此问题。
function getJSONFriendlyString(text) {
return text.replace(/"/g, "\\\"");
}
Hope that helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.