我正在写一个客户端的API ...如何在PHP中使用Zend Json Json :: decode(...)解码unicode编码的JSON?
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();
...并获得以Unicode编码的JSON这样的:
...{"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_decode(...)
解码。但我没有找到任何适当的方法来在PHP中做到这一点(没有可疑的基于正则表达式的方法)。
现在,我与Zend\Json\Json::decode(...)
尝一尝并得到以下错误:
/var/www/path/to/project/vendor/zendframework/zend-json/src/Json.php:243
Decoding failed: Syntax error
如何得到一个Unicode编码的JSON解码的Zend\Json
?
编辑
只是通知,该JSON坏了。它分为两部分。字符串以1f9e
开头,然后是第一部分,然后是字符串\u043
,然后是第二个内容部分,然后是0
。
1f9e <-- What is it?
{"_li...
\u043 <-- What is it?
1a6...
tfoli <-- What is it?
0
确定的JSON是有效的? –
我也用邮差测试过它。所以,是的,我认为,我可以肯定,这是有效的。但编码。 – automatix
等一下,也许你是对的。只需将Postman的“漂亮”视图的API调用输出放到'Json :: decode(...)'作品中即可。然后我从“原始”视图输入相同的输出。我预计它不会工作。但实际上它也起作用。所以,它真的好像是我从'Zend \ Http \ Response#getContent()'得到的JSON的一个问题。 – automatix