1

Is there a way to embed HTML into JSON?

I have formatted HTML data in my database. I am querying this database and serializing the response to JSON. However, when I incorporate this JSON into my web page, its not what I expect!

SQL returned data:

<b>Trolo:</b><br/><b><a target="_blank" class="trololo" href="http://trololooo">TROLOLOLOOO</a></b>

JSON returned data:

"TROLO":"\u003cb\u003eTrolo:\u003c/b\u003e\u003cbr/\u003e\u003cb\u003e\u003ca target=\"_blank\" class=\"trololo\" href=\"http://trololooo\"

Should I be doing some kind of encoding of the HTML data when it is serialized into JSON?

DavidRR
  • 18,291
  • 25
  • 109
  • 191

1 Answers1

1

Use URL-encoding and URL-decoding to encode and decode your HTML.

This might also be helpful: https://stackoverflow.com/a/4292961/1182823

In your scenario, it seems you'll have to do the encoding on your server-side (e.g. with PHP before saving data in the database).

Community
  • 1
  • 1
Adil Malik
  • 6,279
  • 7
  • 48
  • 77