json 字符串可以包含 HTML 标签吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2151956/
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
Can a json string contain HTML tags with it?
提问by ACP
Consider this my json string,
考虑一下我的 json 字符串,
[{
"Mat_id": "2",
"Mat_Name": "Steel",
"Measurement": "mm",
"Description": "Steel"
}]
Can i add HTML Tags inside this json string like this,
我可以像这样在这个 json 字符串中添加 HTML 标签吗,
[{
"Mat_id": "2",
"Mat_Name": "Steel",
"Measurement": "<bold>mm</bold>",
"Description": "Steel"
}]
- Whether this is a valid json string?
- When Eval('('+ thisstring +')') will raise an issue? If so what is it?
- 这是否是有效的 json 字符串?
- 当 Eval('('+ thisstring +')') 会引发问题?如果是这样,那是什么?
I am doing so because i will pass this json object to a yui datatable which consumes json datasource...
我这样做是因为我会将这个 json 对象传递给一个使用 json 数据源的 yui 数据表...
EDIT:
编辑:
This my resulted json string,
这是我的结果 json 字符串,
{
"Table": [{
"Mat_id": "2",
"Mat_Name": "Jully",
"Measurement": "<bold>Inches</bold>",
"Description": "Gully"
}, ]
}
But i didnt get my Measurement column values in bold...
但是我没有以粗体显示我的测量列值...
采纳答案by Aaronaught
Technically, yes, you can do that... practically, I'd be a bit concerned if there were HTML markup in my data. What else might be in there? Smells like an XSS vulnerability.
从技术上讲,是的,您可以这样做...实际上,如果我的数据中有 HTML 标记,我会有点担心。里面可能还有什么?闻起来像 XSS 漏洞。
回答by Thiago Belem
Yeah.. no problem with that. :)
是的。。没问题。:)
回答by Paras
use Encoder.js from http://code.google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176
使用来自http://code.google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176 的Encoder.js
when getting data use
获取数据使用时
Encoder.htmlDecode(value);
Encoder.htmlDecode(value);
and when passing data use
并在传递数据时使用
Encoder.htmlDecode(value);
Encoder.htmlDecode(value);