What’s the right way to decode a string that has special HTML entities in it? [duplicate]

Say I get some JSON back from a service request that looks like this:

{
    "message": "We're unable to complete your request at this time."
}

I’m not sure why that apostraphe is encoded like that ('); all I know is that I want to decode it.

Here’s one approach using jQuery that popped into my head:

function decodeHtml(html) {
    return $('<div>').html(html).text();
}

That seems (very) hacky, though. What’s a better way? Is there a “right” way?

7 Answers
7

Leave a Comment