Converting & to & using Jquery/JS
Question
A lot of answers to this question are to use replace()
but I need a generic solution.. I am using an API and I get some JSON code, I use JQuery's $.each
to go through the data but then the ampersands get escaped into &
, I am not sure what other kinds of entities I can find, so I need to convert the &
and any other possible entities (like "
, <
etc..) to their corresponding characters.
Solution
I don't quite understand the scenario, but if the entities are being returned within the JSON, using .html()
instead of .text()
to send them out to the document works fine.
It's the "then the ampersands get escaped" part that's throwing me off. I don't see where in the flow of retrieving JSON and then running it through an .each() iterator that the entities could get converted.
OTHER TIPS
You should investigate where the &
codes come from.
There is nothing in the $.each
or the code that handles JSON parsing that HTML encodes the data, so it's either added by some specific code that you have, or it's already there when you get the JSON.