简体   繁体   中英

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.

I am using $.getJSON to read the JSON file locally in my project. It will return the JSON object. Using that object i am displaying the labels/text content in Javascript. While displaying the content with html characters it is not loading properly, still its show html characters.

Content inside 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

  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.

Before printing, You have to replace line break
with '\\n'.

Suppose if you take your content to a variable mycontent then you can do it as:

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

Then print 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() ?

Demo fiddle: http://jsfiddle.net/lparcerisa/q3hmjg7w/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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