admin管理员组文章数量:1307005
I have html in json which has already been encoded (by c#) to :
{"itemID":6,"GlossaryWord":"ante","GlossaryDescription":"\u003cp\u003e\r\n\t\u003cstrong\u003eUp the ante\u0026nbsp;\u003c/strong\u003e\u003c/p\u003e\r\n\u003cul\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cstrong\u003eHere\u003c/strong\u003e\u003c/li\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cstrong\u003eis\u0026nbsp;\u003c/strong\u003e\u003c/li\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cb\u003esomething\u003c/b\u003e\u003c/li\u003e\r\n\u003c/ul\u003e\r\n","CategoryID":6}
But how would I decode GlossaryDescription in JavaScript/AngularJS so that it displays the html tags like <p>
& <strong>
etc? Thanks as always :)
I have html in json which has already been encoded (by c#) to :
{"itemID":6,"GlossaryWord":"ante","GlossaryDescription":"\u003cp\u003e\r\n\t\u003cstrong\u003eUp the ante\u0026nbsp;\u003c/strong\u003e\u003c/p\u003e\r\n\u003cul\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cstrong\u003eHere\u003c/strong\u003e\u003c/li\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cstrong\u003eis\u0026nbsp;\u003c/strong\u003e\u003c/li\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cb\u003esomething\u003c/b\u003e\u003c/li\u003e\r\n\u003c/ul\u003e\r\n","CategoryID":6}
But how would I decode GlossaryDescription in JavaScript/AngularJS so that it displays the html tags like <p>
& <strong>
etc? Thanks as always :)
-
2
There is nothing special you have to do: jsfiddle/7cwowxr7. The string contains HTML, just do with it whatever you want to do (the
\uxxxx
escape sequences are already "decoded" by the JSON parser). – Felix Kling Commented Jul 7, 2015 at 16:42 - What code are you using to get the HTML from JSON and add it to the page? – Trisped Commented Jul 7, 2015 at 18:01
- AngularJS, im looping though the json and creating: <a class=\"gobig tooltip\" " + "title=\"" + data[i].GlossaryDescription + "\"> – CerIs Commented Jul 7, 2015 at 21:48
2 Answers
Reset to default 6You don't need to do anything. The string "\u003cp\u003e"
(for example) isn't actually encoded in a way that needs decoding—it's exactly equivalent to "<p>"
. If you type "\u003cp\u003e" == "<p>"
in your console you'll see it return true
. Since the "encoded" string is exactly the same as the non-"encoded" string, you can use them exactly the same way. Take a look:
var obj = {"itemID":6,"GlossaryWord":"ante","GlossaryDescription":"\u003cp\u003e\r\n\t\u003cstrong\u003eUp the ante\u0026nbsp;\u003c/strong\u003e\u003c/p\u003e\r\n\u003cul\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cstrong\u003eHere\u003c/strong\u003e\u003c/li\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cstrong\u003eis\u0026nbsp;\u003c/strong\u003e\u003c/li\u003e\r\n\t\u003cli\u003e\r\n\t\t\u003cb\u003esomething\u003c/b\u003e\u003c/li\u003e\r\n\u003c/ul\u003e\r\n","CategoryID":6};
document.write(obj.GlossaryDescription);
Try:
var htmlToUse = "<a class=\"gobig tooltip\" "
+ "title=\""
+ data[i].GlossaryDescription
+ "\">"
+ data[i].GlossaryWord.trim().toLowerCase()
+ ",</a>";
$('#HtmlParentContainer').html(htmlToUse);
As Jordan pointed out, you do not need to change the content of the fields. All you need to do is use JavaScript (and jQuery) to replace the existing HTML with the new HTML you generated. If you do not want to use JavaScript's innerHTML then you can use jQuery's html().
JSFiddle
Note: your GlossaryDescription
field should not have HTML in it if you are applying it to an element attribute like title
. Also, the code above acts as if data is an array, but your example does not. As a result, the JSFiddle drops the [i]
本文标签: javascriptDecode html in jsonStack Overflow
版权声明:本文标题:javascript - Decode html in json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741832433a2400013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论