简体   繁体   中英

Can't decode JSON value in PHP

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":"\س\ل\ا\م","code":200},"data":{"title":"\س\ل\ا\م","pronunciation":"","text":"(\س\َ) [ \ع . ] (\م\ص \ل .) 1 - \د\ر\و\د \گ\ف\ت\ن . 2 - \ب\ی \گ\ز\ن\د \ش\د\ن . 3 - \گ\ر\د\ن \ن\ه\ا\د\ن . \؛ ~ \ع\ل\ی\ک \د\ر\و\د \ب\ر \ت\و \ب\ا\د. \؛ ~ \ع\ل\ی\ک\م \د\ر\و\د \ب\ر \ش\م\ا. ","source":"\ف\ر\ه\ن\گ \ف\ا\ر\س\ی \م\ع\ی\ن | \و\ا\ژ\ه \ی\ا\ب","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...

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;
}

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

Please check with this.

echo htmlentities((string)$results); 

Also reffer Problem with json_decode PHP

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));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM