5

I found no solution for this.

I have an API witch give me JSON resposne,

http://api.vajehyab.com/v2/public/?q=%D8%B3%D9%84%D8%A7%D9%85&developer=stackoverflow

which is something like this,

{"search":{"q":"\u0633\u0644\u0627\u0645","code":200},"data":{"title":"\u0633\u0644\u0627\u0645","pronunciation":"","text":"(\u0633\u064e) [ \u0639 . ] (\u0645\u0635 \u0644 .) 1 - \u062f\u0631\u0648\u062f \u06af\u0641\u062a\u0646 . 2 - \u0628\u06cc \u06af\u0632\u0646\u062f \u0634\u062f\u0646 . 3 - \u06af\u0631\u062f\u0646 \u0646\u0647\u0627\u062f\u0646 . \u061b ~ \u0639\u0644\u06cc\u06a9 \u062f\u0631\u0648\u062f \u0628\u0631 \u062a\u0648 \u0628\u0627\u062f. \u061b ~ \u0639\u0644\u06cc\u06a9\u0645 \u062f\u0631\u0648\u062f \u0628\u0631 \u0634\u0645\u0627. ","source":"\u0641\u0631\u0647\u0646\u06af \u0641\u0627\u0631\u0633\u06cc \u0645\u0639\u06cc\u0646 | \u0648\u0627\u0698\u0647 \u06cc\u0627\u0628","permalink":"?q=%D8%B3%D9%84%D8%A7%D9%85"},"error":{"message":"","reason":""},"ads":{"text":"","url":""}}

It doesn't seem to have problem ,I can decode it in other tools but PHP, I can't decode it in PHP

$json = file_get_contents('http://api.vajehyab.com/v2/public/?q='.urlencode('سلام').'&developer=stackoverflow');
var_dump(json_decode($json)); // null

I hope somebody could help me with this, thank you...

Emad Ghasemi
  • 396
  • 1
  • 3
  • 10

5 Answers5

2

Taken from php docs http://php.net/manual/bg/function.json-last-error.php Run this code and let us know if you receive any errors.

json_decode($string);

switch(json_last_error())
{
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
}
Stanimir Dimitrov
  • 1,872
  • 2
  • 20
  • 25
2

EDIT2: In PHP 5.3+ this seems to work too:

$json = file_get_contents('http://api.vajehyab.com/v2/public/?q='.urlencode('سلام').'&developer=stackoverflow');

$unescaped = json_encode(preg_replace_callback('/\\\\u(\w{4})/', function ($matches) {
    return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8');
}, $json));
var_dump($unescaped);

Taken from: https://stackoverflow.com/a/24933162/2433843

EDIT3: wrapped the preg_replace_callback in json_encode

Community
  • 1
  • 1
Francesco de Guytenaere
  • 4,443
  • 2
  • 25
  • 37
  • No, `json_decode` still return null ! `jsonlint` validate both of them but PHP say it's not :| – Emad Ghasemi Jul 01 '15 at 11:31
  • Sorry @EmadGhasemi, try this: `$unescaped = json_encode(preg_replace_callback('/\\\\u(\w{4})/', function ($matches) { return html_entity_decode('' . $matches[1] . ';', ENT_COMPAT, 'UTF-8'); }, $json));` – Francesco de Guytenaere Jul 01 '15 at 11:35
  • You're right, it feels kind of hacky, but it seems it is interpreted as if it was never properly encoded in the first place (I know, it does work in JS Linters). Perhaps it's better to solve it server-side, but perhaps it's a flaw in the PHP json implementation. This seems to work for me though, but I understand if it is too dirty to implement. I'm interested to see the clean solution! – Francesco de Guytenaere Jul 01 '15 at 11:44
  • It seems that they are using php 5.5.9, and somebody in the comments mentioned it worked with no problem in 5.5.21. Maybe something is going wrong with `json_encode` in 5.5.x :-/ – Emad Ghasemi Jul 01 '15 at 11:50
  • Ok. Did you try above code? Sorry if it doesn't work; here it works on PHP 5.6.4-4ubuntu6 (local development environment) – Francesco de Guytenaere Jul 01 '15 at 11:53
0

Please check with this.

echo htmlentities((string)$results); 

Also reffer Problem with json_decode PHP

Community
  • 1
  • 1
Jagadeesh
  • 734
  • 6
  • 26
0

I think the problem is from file_get_contents function. json_decode works fine when input is string. Below code run correctly.

$json = '{"search":{"q":"\u0633\u0644\u0627\u0645","code":200},"data":{"title":"\u0633\u0644\u0627\u0645","pronunciation":"","text":"(\u0633\u064e) [ \u0639 . ] (\u0645\u0635 \u0644 .) 1 - \u062f\u0631\u0648\u062f \u06af\u0641\u062a\u0646 . 2 - \u0628\u06cc \u06af\u0632\u0646\u062f \u0634\u062f\u0646 . 3 - \u06af\u0631\u062f\u0646 \u0646\u0647\u0627\u062f\u0646 . \u061b ~ \u0639\u0644\u06cc\u06a9 \u062f\u0631\u0648\u062f \u0628\u0631 \u062a\u0648 \u0628\u0627\u062f. \u061b ~ \u0639\u0644\u06cc\u06a9\u0645 \u062f\u0631\u0648\u062f \u0628\u0631 \u0634\u0645\u0627. ","source":"\u0641\u0631\u0647\u0646\u06af \u0641\u0627\u0631\u0633\u06cc \u0645\u0639\u06cc\u0646 | \u0648\u0627\u0698\u0647 \u06cc\u0627\u0628","permalink":"?q=%D8%B3%D9%84%D8%A7%D9%85"},"error":{"message":"","reason":""},"ads":{"text":"","url":""}}';
var_dump(json_decode($json));
Hossein
  • 3,755
  • 2
  • 29
  • 39
0

Please encode your data to utf8 format before encoding it through json_encode() php function. I hope , it helps. [utf8_encode()]

Lakin Mohapatra
  • 1,097
  • 11
  • 22