Is it possible to write an HTML string inside JSON?
Which I want to write like below in my JSON file:
[ { "id": "services.html", "img": "img/SolutionInnerbananer.jpg", "html": "<h2class="fg-white">AboutUs</h2><pclass="fg-white">developing and supporting complex IT solutions.Touchingmillions of lives world wide by bringing in innovative technology </p>" } ]
The problem with HTML inside JSON is that HTML elements have properties that must use double quotes. But as soon as you insert double quotes it's going to cause syntax error because JSON can only do double quotes.
You're HTML values are OK, but the keys of the JSON object must be enclosed in quotes. string. quotation marks. Also, if you output this JSON object inside the script tags of an HTML page, you must escape the "</" sequence of HTML closing tags, as per this appendix in the HTML 4 specification.
What you want to do is called serializing. var json = JSON. stringify({html:html}) , that is JSON. What you have is simply a javascript object, no such thing as a "JSON object".
You should escape the characters like double quotes in the html string by adding "\"
eg: <h2 class=\"fg-white\">
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With