4

I recently just solved the issue of outputting foreign characters using utf8_decode function in this thread: How do I convert, display and store this characters in PHP?

It works by directly echoing the results out but now I have this json_encode function to pass to jquery for the results. Json_encode is escaping my data to something like this:

{"title":"\u90ed\u5bcc\u57ce - \u641c\u7d22"}

How do I json_decode from jquery? Thank you for any advice.

Community
  • 1
  • 1
pakito
  • 387
  • 2
  • 3
  • 17
  • What do you mean by asking *How do I json_decode from jquery?* ? – hakre Jun 27 '11 at 19:18
  • Sorry but I can't understand the answer to the question you link. How can `utf8_encode()` be of any help in a site that uses UTF-8. That function produces `ISO-8859-1` output! – Álvaro González Jun 27 '11 at 19:27
  • Hi Alvaro, if you refer to my previous thread: http://stackoverflow.com/questions/6495158/how-do-i-convert-display-and-store-this-characters-in-php You can understand why I need to utf8_decode the string. Only when I utf8_decode the string, I can echo the foreign characters out directly with PHP. But now I realised that decoding it and pass to json will have this issue :( – pakito Jun 27 '11 at 19:38
  • refer http://stackoverflow.com/questions/5396560/how-do-i-convert-special-utf-8-chars-to-their-iso-8859-1-equivalent-using-javasc/26887395#26887395 might help you! – Vipin Kohli Nov 12 '14 at 12:56

3 Answers3

5

jQuery offers the parseJSON method straight from the jQuery object:

var data = $.parseJSON('{"title":"\u90ed\u5bcc\u57ce - \u641c\u7d22"}');

For fetching data via AJAX, though, $.getJSON will run this internally and pass the result of $.parseJSON as the request's final result.

Matchu
  • 83,922
  • 18
  • 153
  • 160
  • I have this parseJSON in jquery but apparently not working :( – pakito Jun 27 '11 at 19:29
  • @pakito: Well, the method is failing because it's coming out as something like `$.parseJSON({title: "the title"})`. `parseJSON` expects a string, not an object. But! Since JSON stands for JavaScript Object Notation, and this is trusted content, you can just drop the result of `json_encode` into the script: `'var obj = ' . $title . ';'` – Matchu Jun 27 '11 at 19:37
  • But apart from title, I have other data hence as an array, hence I need to use json_encode to pass to jquery? – pakito Jun 27 '11 at 19:41
  • Any one with any help is greatly appreciated. – pakito Jun 27 '11 at 19:56
2

Either you are writing it directly into JS, in which case do nothing or you are using one of ajax methods in jQuery, in which case just specify the dataType to be "json"

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Hi you are right Quentin. It works once I set this. Thank you very and thanks all those who replied for help. Thanks again. – pakito Jun 27 '11 at 20:24
1

try

var obj = JSON.parse('{"title":"\u90ed\u5bcc\u57ce - \u641c\u7d22"}');
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • I got Error: JSON.parse: unexpected character The testing code can be save and run directly here: http://pastie.org/2130655 – pakito Jun 27 '11 at 19:40