1

i am useing an API getting some json objects.

The one i need is: results_html

When you look at the name you see it will be html. My problem is, in the json it is unicode encoded. When i try to decode it some characters are wrong!

I tried out JSON_UNESCAPED_UNICODE and i tried out some str_replace() things like this:

$json_object = str_replace('\u2122', 'e2 84 a2;', str_replace('\u2605', 'e2 98 85', file_get_contents($url)));
        $json_decoded = json_decode($json_object);

But i didnt get any good result! Sometimes the total json crashes or the chars are bad anyway.

EDIT: Here is an example: WHen i use:

$json_object = file_get_contents($url);
$json_decoded = json_decode($json_object);
$html = $json_decoded -> results_html;

the $html will echo this in an utf8 encoded html body: â StatTrakâ¢

Does somebody have any idea?

Thank You so far.

fr3ddyf
  • 73
  • 8
  • JSON is *always* in Unicode, by the way. – Joey Apr 17 '14 at 09:46
  • Are the character *bad* wehn you do output them? Have you considered using unicode in your output/database as well? – Andresch Serj Apr 17 '14 at 09:54
  • @Јοеу ok ok why do i get problems then? – fr3ddyf Apr 17 '14 at 09:57
  • @AndreschSerj yes they are bad when i output them. is there no way to transform them from unicode to utf8? – fr3ddyf Apr 17 '14 at 09:57
  • maybe you need to read up on character encoding and what unicode actually is http://stackoverflow.com/questions/643694/utf-8-vs-unicode – Andresch Serj Apr 17 '14 at 10:01
  • if you don't want to read the whole SO Q/A read this instead: http://www.joelonsoftware.com/articles/Unicode.html – Andresch Serj Apr 17 '14 at 10:02
  • @AndreschSerj thank you i will read this but do you have any short tip for me how i can solve my problem? – fr3ddyf Apr 17 '14 at 10:14
  • We should *avoid extended discussions in comments*. I think your problem is somewhere between not using utf-8 thru out your whole process and not understanding unicode. Read the article. Get your utf-8 straight. And have a nice easter weekend :D – Andresch Serj Apr 17 '14 at 10:22
  • @AndreschSerj haha ok =) yes i will! I hope you too! thank you – fr3ddyf Apr 17 '14 at 10:25
  • `json_decode()` does give you UTF-8 output: `json_decode('"\u2122"')` gives the string `"\xE2\x84\xA2"`. Maybe your input is wrong or maybe you're printing the results wrong, or maybe your output HTML page isn't marked with a meta tag as being UTF-8. – bobince Apr 17 '14 at 12:23
  • @bobince yes the meta tag was the problem! i solved it! Thank you guys! – fr3ddyf Apr 20 '14 at 11:10

0 Answers0