关于PHP Array到JSON序列化我确实有一个奇怪的问题:
我有一个多层的数组,并且有一定的大小,它运行得很好。但在某些时候,返回的JSON对象中缺少右大括号。 这是返回的标题。
Date: Fri, 29 Dec 2017 12:42:39 GMT
Server: Apache/2.2.22 (Debian)
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, userid, dealerid
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
X-Powered-By: PHP/5.4.45
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 8503
X-UA-Compatible: IE=edge
Content-Type: application/json;charset=utf-8
使用SLIM 3 Framework为简单的REST API构建API。
API上的设置:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
...
public function listFailedOrders(Request $request, Response $response, array $args) {
$arr = [];
//algorithm to fill array
//verified that the array has data that is not malformed in any way!
$responseData = [];
$responseData['count'] = count($arr);
$responseData['data'] = $arr;
$response = $response->withJson($responseData, 200);
return $response;
}
退回数据:
{
"count": "41",
"data": [
{
.... JSON DATA ....
},
{
.... More JSON Data ....
}
]
//Missing End curly Bracket
&#13;
我使用Insomnia Rest客户端来检查这一点,直到这一点,我唯一的解决方案是在接收端添加一个结束的花括号。
TLDR: 我将一个大的(不限制到达对象)序列化为JSON,但是尾随的花括号被修剪。问题出在哪里?
答案 0 :(得分:0)
问题是预设的“内容长度标头”切断了最后一个花括号。 这是一个严重的错误。