Is it possible to write an HTML string inside JSON?
Which I want to write like below in my JSON file:
[
{
\"id\": \"services.html\",
Just encode html using Base64 algorithm before adding html to the JSON and decode html using Base64 when you read.
byte[] utf8 = htmlMessage.getBytes("UTF8");
htmlMessage= new String(new Base64().encode(utf8));
byte[] dec = new Base64().decode(htmlMessage.getBytes());
htmlMessage = new String(dec , "UTF8");
in json everything is string between double quote ", so you need escape " if it happen in value (only in direct writing) use backslash \
and everything in json file wrapped in {} change your json to
{
[
{
"id": "services.html",
"img": "img/SolutionInnerbananer.jpg",
"html": "<h2 class=\"fg-white\">AboutUs</h2><p class=\"fg-white\">developing and supporting complex IT solutions.Touching millions of lives world wide by bringing in innovative technology</p>"
}
]
}
Lets say I'm trying to render the below HTML.
let myHTML = "<p>Go to this <a href='https://google.com'>website </b></p>";
JSON.parse(JSON.stringify(myHTML))
This would give you a HTML element which you can set using innerHTML.
Like this
document.getElementById("demo").innerHTML = JSON.parse(JSON.stringify(myHTML));
People are storing their HTML as an object here. However the method I suggested does the same without having to use an Object.
The easiest way is to put the HTML inside of single quotes. And the modified json object is as follows:
[
{
"id": "services.html",
"img": "img/SolutionInnerbananer.jpg",
"html": '<h2 class="fg-white">AboutUs</h2><p class="fg-white">developing and supporting complex IT solutions.Touchingmillions of lives world wide by bringing in innovative technology </p>'
}
];
Fiddle.
And the best way is to esacape the double quotes and other characters that need to be escaped. The modified json object is as follows:
[
{
"id": "services.html",
"img": "img/SolutionInnerbananer.jpg",
"html": "<h2 class=\"fg-white\">AboutUs</h2><p class=\"fg-white\">developing and supporting complex IT solutions.Touchingmillions of lives world wide by bringing in innovative technology </p>"
}
];
Fiddle.
It is possible to write an HTML string in JSON. You just need to escape your double-quotes.
[
{
"id": "services.html",
"img": "img/SolutionInnerbananer.jpg",
"html": "<h2class=\"fg-white\">AboutUs</h2><pclass=\"fg-white\">CSMTechnologiesisapioneerinprovidingconsulting,
developingandsupportingcomplexITsolutions.Touchingmillionsoflivesworldwidebybringingininnovativetechnology,
CSMforayedintotheuntappedmarketslikee-GovernanceinIndiaandAfricancontinent.</p>"
}
]
You can, once you escape the HTML correctly. This page shows what needs to be done.
If using PHP, you could use json_encode()
Hope this helps :)