PHP的JSON恩code大阵列被截断 [英] php json encode large arrays gets truncated

查看:275
本文介绍了PHP的JSON恩code大阵列被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到与json_en code显示JSON的一个问题。问题是,它被某一个点之后截断。我猜围绕2500线。我读过很多Q&放大器;一个是在计算器和其他人则建议增加内存限制。我已经增加了它32M的
它仍然被截断而且我不认为这是我一直回荡着内存使用情况,并与真正的问题,我得到3.5MB和假我得到1.5MB我也尝试过了1000,但是,直到某一点上它编码仍将截断。

我的JSON看起来像这样

{ID:1},{词:},{位置:0},{长:0},{Pdf_Id:1 }

只是2000多

我的code是这样的:

JSON文件

 回声json_en code(array_values​​($ DB-GT&; getallqueryjsoncountStart(words_table,Allwords,4000,0)));
公共职能getallqueryjsoncountStart($表,$ jsonparentname,$计数$开始)
{    $这个 - >的SQLQuery =选择$表LIMIT $开始,$数*;
    $这个 - >语句= $这个 - > conn->查询($这个 - >的SQLQuery);
    为($ I = 0; $ I< $这个 - > stmt->列数(); $ I ++){
        $山口= $这个 - > stmt-> getColumnMeta($ I);
        $列[] = $ COL ['名'];
    }
    $这个 - > initcnt = 0;
    $ getqueryJson =阵列();    而($这个 - >排= $这个 - > stmt->取())
    {
                为($ X = 0; $ X<的sizeof($列); $ X ++)
                {
                    $ getqueryJson [] [$列[$ X] = $这个 - >行[$列[$ X]];
                }
    }
    返回$ getqueryJson;
}


解决方案

如果在言一栏确实包含一些文本有可能包含的价值中的一些非法字符。

将以下选项添加到json_en code函数:

  JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP

有关人员对每个确实看到手册

也。它看起来像你的方式是构建在 $ getqueryJson 数组是不正确。尝试改变如下:

  $ getqueryJson =阵列();
而($这个 - >排= $这个 - > stmt->取())
{
    $行=阵列()
    为($ X = 0; $ X<计数($列); $ X ++)
    {
        $行[$列[$ X] = $这个 - >行[$列[$ X]];
    }
    $ getqueryJson [] = $行;
}
返回$ getqueryJson;

调用方法,像这样。

  $ TABLE_DATA = $ DB-GT&; getallqueryjsoncountStart(words_table,Allwords,4000,0);
回声json_en code($ TABLE_DATA,
                 JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);

这将产生一个JSON字符串格式如下。

  [{ID:1,词:,位置:0,长度:0,Pdf_Id: 1},
 {ID:2,词:,位置:0,长度:0,Pdf_Id:2},
 {ID:3,词:,位置:0,长度:0,Pdf_Id:3},
 ...
 {ID:4000,词:,位置:0,长度:0,Pdf_Id:4000}]

如果问题仍然存在,尝试调用 json_last_error () 呼叫后立即 json_en code

I am encountering a problem displaying json with json_encode. Problem is it gets truncated after a certain point. I'm guessing around 2500 lines. I've read a lot of q&a's in stackoverflow and others suggesting to increase memory limit. I've increased it already to 32m and it still gets truncated furthermore I don't think it's the problem for i've echoed the memory usage and with true i get 3.5mb and with false i get 1.5mb I also tried encoding it by 1000 but up until a certain point it still truncates.

my json looks like this

{"Id":"1"},{"Words":""},{"Position":"0"},{"Length":"0"},{"Pdf_Id":"1"}

just 2000 more

My code goes something like this:

json file

echo json_encode(array_values($db->getallqueryjsoncountStart("words_table","Allwords",4000,0)));


public function getallqueryjsoncountStart($table,$jsonparentname,$count,$start)
{

    $this->sqlquery = "select * from $table LIMIT $start,$count";
    $this->stmt = $this->conn->query($this->sqlquery);
    for ($i = 0; $i < $this->stmt->columnCount(); $i++) {
        $col = $this->stmt->getColumnMeta($i);
        $columns[] = $col['name'];
    }
    $this->initcnt = 0;
    $getqueryJson = array();

    while($this->row = $this->stmt->fetch()) 
    {
                for($x = 0;$x < sizeof($columns);$x++)
                {
                    $getqueryJson[][$columns[$x]] = $this->row[$columns[$x]];
                }
    }
    return $getqueryJson;
}

解决方案

If the "Words" column actually contains some text there maybe some illegal character contained within the value.

Add the following options to the json_encode function:

JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP

For a description of what each does see the manual page

Also. it looks like the way you are structuring the $getqueryJson array is incorrect. Try changing as follows

$getqueryJson = array();
while($this->row = $this->stmt->fetch()) 
{
    $row = array()
    for($x = 0;$x < count($columns);$x++)
    {
        $row[$columns[$x]] = $this->row[$columns[$x]];
    }
    $getqueryJson[] = $row;
}
return $getqueryJson; 

Call the method like so.

$table_data = $db->getallqueryjsoncountStart("words_table","Allwords",4000,0);
echo json_encode($table_data, 
                 JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);

This will produce a JSON string with the following format.

[{"Id":"1", "words":"", "Position":"0","Length":"0","Pdf_Id":"1"},
 {"Id":"2", "words":"", "Position":"0","Length":"0","Pdf_Id":"2"},
 {"Id":"3", "words":"", "Position":"0","Length":"0","Pdf_Id":"3"},
 ...
 {"Id":"4000", "words":"", "Position":"0","Length":"0","Pdf_Id":"4000"}]

If the problem persists, try calling json_last_error() right after calling json_encode.

这篇关于PHP的JSON恩code大阵列被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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