1

Browser: FF Latest / Chrome Latest
jQuery: 1.x edge

An ajax request produces this response (sent as plain text, single line, valid JSON (produced by Gson) handled by jQuery as plain text).

{
  "fromSymbol": "\\u04b0",
  "toCurrency": "AUD",
  "toSymbol": "\\u0024",
  "convFactorPrecise": 0.171346,
  "amount": 38020.0,
  "convertedAmountPrecise": 6514.57,
  "convertedAmountFormatted": "6,514.57"
}

(Formatted here for easy reading, but it arrives as single line, minified).

Then I use the following line to convert the plain text into an object literal:

var currObj = $.parseJSON($.trim(thatStringUpThere));

The problem is, currObj.toSymbol is now literally '\u0024', and not '$'.

  • Consider that I cannot change the response type and handle as json
  • And given that I only have that plain string to work with in jQuery's success method

Q: how would I dump $ into a dom node?

(Currently, jquery's .html() and .text() keep dumping '\u0024' to the dom, and not the $ symbol).

Thanks.

  • Which browser/jQuery are you using? It works as expected for me on jQuery 2.0.3/Chrome 36. – joews Aug 12 '14 at 08:29
  • [link](http://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode) – user3917789 Aug 12 '14 at 08:31
  • @joews Details added. I'm on jQuery 1.x edge. What code did you use to dump '\\u0024' as dollar ($) to the dom? –  Aug 12 '14 at 08:35
  • On a page with an existing `h1` tag: `var currObj = $.parseJSON($.trim(s)); $('h1').text(currObj.toSymbol);` – joews Aug 12 '14 at 08:44

1 Answers1

0

Simple solution:

unescape(JSON.parse($.trim(thatStringUpThere)));

More elegant solution:

var r = /\\u([\d\w]{4})/gi; 
x = unescape (x.replace(r, function (match, grp) { 
  return String.fromCharCode(parseInt(grp, 16)); 
} ));
RAM
  • 2,413
  • 1
  • 21
  • 33