输出到HTTP客户端时,JSON字符串(从大型数组编码)被截断 [英] JSON string (encoded from large array) got truncated when output to HTTP client

查看:861
本文介绍了输出到HTTP客户端时,JSON字符串(从大型数组编码)被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,输出到HTTP客户端的JSON字符串在close标签附近被截断了.

I met an issue that the JSON string output to HTTP client was truncated near to close tag.

一个例子

a.希望看到-... ..."last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":null}

a. expect to see - ... ... "last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":null}

b.实际上是-... ..."last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":nul

b. actually it is - ... ..."last_update":"2014-06-10 20:46:38","garden_id":"1"}],"message":nul

由于某些原因,最后两个字符被截断了!!!

The last two characaters were truncated for some reason!!!

我尝试了Chrome上的Postman和控制台上的curl,它们的输出都是相同的.因此,看起来不是浏览器特定的问题.我的应用程序中的JSON字符串来自关联数组上的PHP json_encode. PHP代码使用在Apache上运行的CodeIgniter框架.我尝试将json字符串写入http输出之前的文件中,文件内容是100%正确的.因此,这不是PHP中的json编码问题.

I tried both Postman on Chrome and curl on a console, all the same output. So looks not a browser specific issue. The JSON string in my application comes from a PHP json_encode on an associative array. The PHP code is using CodeIgniter framework running on Apache. I tried to write the json string into a file before http output, the file content is 100% correct. So it is not a json encoding issue in PHP.

PHP非常简单.我已经从数据库查询中构建了下面的数组(即$ finaldata)

The PHP is pretty straightforward. I have build below array (namely $finaldata) from database query

{
    "success": true,
    "data": [
        {
            "file_id": "1",
            "title": "xxx",
            "create_date": "2014-05-18 21:30:19",
            "auditor": "1",
            "status": "1",
            "last_updater": null,
            "last_update": "2014-06-10 20:43:14",
            "garden_id": "1"
        },
        {
            "file_id": "2",
            "title": "yyy",
            "create_date": "2014-05-18 21:30:19",
            "auditor": "1",
            "status": "1",
            "last_updater": null,
            "last_update": "2014-06-10 20:43:14",
            "garden_id": "1"
        }
    ],
    "message": null
}

数据"有一个子数组,它可能是一个长数组,取决于数据库记录.然后将变量$ finaldata传递给具有以下常规逻辑的输出函数:

the "data" has a sub array and it could be a long array depends on database records. Then the variable $finaldata is passed to a output function that has below general logic:

header('Content-Type: '.$this->_supported_formats[$this->response->format]);
$output = $this->format->factory($finaldata)->{'to_'.$this->response->format}();
header('Content-Length: ' . strlen($output));

输出功能由RESTful库从 https://github.com/philsturgeon/codeigniter-构建. restserver .在这种情况下,它等于

The output function is built by a RESTful library from https://github.com/philsturgeon/codeigniter-restserver. In this context, it equals to

header('Content-Type: Application/json');
$output = json_encode($finaldata);

但是我发现一个有趣的事情,该问题仅在字符串长度超过8K时才会发生.当我在HTTP响应之前将诸如"ZZZ"之类的随机字符串附加到JSON字符串时,问题就消失了.我不知道背后的原因.这不是正确的破解方法,因为我无法证明8K是真正的门槛.

But I found an interesting thing that the issue only happened if the string length exceeds 8K. And when I append a random string like "ZZZ" to the JSON string before HTTP response, the issue was gone. I don't know the reason behind. It is not a correct hack because I could not prove 8K is a real threshold though.

以前有人遇到过这样的问题吗?任何建议或意见,表示赞赏.

Has anyone met such issue before? Any suggestion or comments are appreciated.

推荐答案

我认为您以UTF-8字符集发送数据,因此请尝试更改此行

I think you send the data in UTF-8 charset so try to change this lines

header('Content-Type: '.$this->_supported_formats[$this->response->format]);
header('Content-Length: ' . strlen($output));

进入这个

header('Content-Type: '.$this->_supported_formats[$this->response->format] . '; charset=utf-8');
header('Content-Length: ' . mb_strlen($output));

如果这不起作用,则根本不发送Content-Length标头.如果您将文件发送到浏览器(作为下载),则此标头仅有用".

If this doesn't work just don't send the Content-Length header at all. This header is "only useful" if you send a file to the browser (as a download).

这篇关于输出到HTTP客户端时,JSON字符串(从大型数组编码)被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆