0

I'm writing a client for an API...

use Zend\Http\Client;
use Zend\Http\Request;
use Zend\Json\Json;
...
$request = new Request();
$request->getHeaders()->addHeaders([
    'Accept-Charset' => 'UTF-8',
    'Accept' => 'application/hal+json',
    'Content-Type' => 'application/hal+json; charset=UTF-8',
]);
$apiAddress = 'http://my.project.tld/categories';
$request->setUri($apiAddress);
$request->setMethod('GET');
$client = new Client();
$response = $client->dispatch($request);
$data = $response->getContent();

... and get a unicode encoded JSON like this:

...{"id":"7","title":"\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438","short_name":"\u0418\u041b\u041b\u042e\u0421\u0422\u0420\u0410\u0426\u0418\u0418"...

First I tried to decode it with json_decode(...). But I didn't find any appropriate method to do this in PHP (without suspect regex based approaches).

Now I'm trying it with Zend\Json\Json::decode(...) and getting following error:

/var/www/path/to/project/vendor/zendframework/zend-json/src/Json.php:243
Decoding failed: Syntax error

How to get a unicode-encoded JSON decoded with Zend\Json?


EDIT

Just notices, that the JSON is broken. It is separated to two parts. The string starts with 1f9e, then the first part, then the string \u043, then the second content part, then 0.

1f9e <-- What is it?
{"_li...
\u043 <-- What is it?
1a6...
tfoli <-- What is it?
0
3
  • Are you sure the JSON is valid? Commented Jul 7, 2017 at 18:46
  • I've also tested it with Postman. So, yes, I think, I can be sure, that it's valid. But encoded. Commented Jul 7, 2017 at 18:47
  • Wait, maybe you are right. Just put the API call's output from Postman's "pretty" view to the Json::decode(...) -- works. Then I put the same output from the "raw" view. I expected it not to work. But actually it have worked as well. So, it really seems to be a problem with the JSON I get from Zend\Http\Response#getContent(). Commented Jul 7, 2017 at 18:56

2 Answers 2

1

Use Zend\Json\Decoder component of ZF2. It has a static method called Decoder::decodeUnicodeString() which decodes unicode characters.

Please have a look at the script here.

Hope this would help you!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! +1 But Actually the problem is in my case in the broken JSON. Please see this question for more information.
0

As soon as I see this json doesn't seem to be broken. Please consider the next code lines:

$data = '{"id":"7","title":"\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438","short_name":"\u0418\u041b\u041b\u042e\u0421\u0422\u0420\u0410\u0426\u0418\u0418"}';

$json = json_decode($data);

header('Content-Type: text/html; charset=utf-8');
echo $json->title;
echo "<br/>";
echo $json->short_name;

the result is:

иллюстрации
ИЛЛЮСТРАЦИИ

1 Comment

Thank you for your answer, but please see the EDIT in my question and also this question, because -- you are right -- the JSON by itself is OK, but it comes with some additional symbols at the beginning and at the end. The JSON between these additional strings is actually valid. But all together gives an invalid JSON string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.