How to decode a Unicode encoded JSON in PHP with Zend \ Json \ Json :: decode (& hellip;)?


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

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

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


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:

Decoding failed: Syntax error

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


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?
\u043 <-- What is it?
tfoli <-- What is it?

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!