簡體   English   中英

PHP 5.4 json_decode生成格式錯誤的json

[英]PHP 5.4 json_decode generates malformed json

關於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

該API是使用SLIM 3 Framework構建的,用於簡單的REST 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 

我使用Insomnia Rest客戶端進行檢查,直到這一點為止,對此問題,我唯一的解決方案是在接收端添加一個大括號。

TLDR:我將一個大的(不是限制到達的對象)序列化為JSON,但尾隨的花括號被修剪了。 問題從何而來?

問題是預設的Content Length Header(內容長度標題)切斷了最后一個花括號。 這是一個嚴重的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM