简体   繁体   English

从JSON文件中读取带有html标记的内容并在javascript中显示

[英]Reading the content with html tags from JSON file and displaying in javascript

I have JSON file which holds labels and content to display in my mobile application. 我有JSON文件,其中包含要在移动应用程序中显示的标签和内容。

I am using $.getJSON to read the JSON file locally in my project. 我正在使用$ .getJSON在项目中本地读取JSON文件。 It will return the JSON object. 它将返回JSON对象。 Using that object i am displaying the labels/text content in Javascript. 使用该对象,我正在用Javascript显示标签/文本内容。 While displaying the content with html characters it is not loading properly, still its show html characters. 当显示带有html字符的内容时,它无法正确加载,但仍显示html字符。

Content inside JSON JSON内的内容

 "CONSENT_EMAIL_REQUEST_BODY"  :   "Please read the below content <br> Welcome to the project <br> you can now use the application."

If i try to print the value of CONSENT_EMAIL_REQUEST_BODY in javascript it still displaying 如果我尝试在javascript中打印CONSENT_EMAIL_REQUEST_BODY的值,它仍会显示

  Please read the below content <br> Welcome to the project <br> you can now use the application.

It is not displaying line by line the break 它不是逐行显示中断
are displaying as it is. 正在按原样显示。 How to replace these html characters to display properly in mobile/browsers. 如何替换这些html字符以在手机/浏览器中正确显示。

Before printing, You have to replace line break 在打印之前,您必须更换换行符
with '\\n'. 与'\\ n'。

Suppose if you take your content to a variable mycontent then you can do it as: 假设如果将内容带到变量mycontent,则可以按以下方式进行操作:

mycontent = mycontent.replace(/<br\s*\/?>/mg,"\n");

Then print mycontent. 然后打印mycontent。 That's it. 而已。

It is working for me with the following code: 它对我有用以下代码:

var json =  jQuery.parseJSON( '{"CONSENT_EMAIL_REQUEST_BODY"  :   "Please read the below content <br> Welcome to the project <br> you can now use the application."}');

$("span").html(json.CONSENT_EMAIL_REQUEST_BODY);

Maybe are you using .text() instead of .html() ? 也许您使用的是.text()而不是.html()

Demo fiddle: http://jsfiddle.net/lparcerisa/q3hmjg7w/ 演示小提琴: http : //jsfiddle.net/lparcerisa/q3hmjg7w/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM