json_encode() 方法是否有内存限制? [英] Is there any memory limit for json_encode() method?

查看:63
本文介绍了json_encode() 方法是否有内存限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图回显一个由数组组成的 json 编码数组,但我不知道它不允许我打印那个东西.这是我的代码:

I am trying to echo a json-encoded array which consist of an array but i dont know it is not letting me print that thing. Here's my code:

<?php

include_once('confi.php');
header('Content-type: application/json');

if ($_SERVER['REQUEST_METHOD'] == "POST")
{
    $lastRecord = isset($_POST['lastRecordID']) ? 
                      mysql_real_escape_string($_POST['lastRecordID']) : "";

    $queryForTotalRec = mysql_query("SELECT customer_id FROM `WebServiceTesting`.`sapphire` ORDER BY customer_id DESC LIMIT 1");
    $total_rec = mysql_fetch_row($queryForTotalRec);

    if($total_rec){
        $queryForAllRecords = "SELECT * FROM `WebServiceTesting`.`sapphire` WHERE customer_ID BETWEEN %d AND %d";
       $get_all_recs = mysql_query(sprintf($queryForAllRecords, $lastRecord, $total_rec[0]));

        $json = array();
        while($row = mysql_fetch_assoc($get_all_recs)){

          $json[] = array("Status" => 1, "NewRecord" => $row);
        }

        print_r($json);
        echo json_encode($json);
   }else{
    $json = array("status" => 0, "Error_Message" => mysql_error());
          echo json_encode($json);
    }
}else{

    $json = array("status" => 0, "Error_Message" => "Request Method not correct");
      echo json_encode($json);

}
@mysql_close($conn);

错误:格式错误的 JSON:意外的A"有时是I"

Errors: Malformed JSON: Unexpected 'A' sometimes 'I'

当我删除 print_r 行时,我得到:没有收到回复当我打印 $json 数组的计数时,我得到的计数为 153,但没有其他输出.

When i am deleting the print_r line iam getting: No response received When i am printing the count of $json array iam getting a count of 153 but NO OTHER output.

我尝试过的事情:

我阅读了一些类似问题的解决方案,您需要使用 array_values()例如:

i read in some solutions to similar problems that u need to use array_values() for e.g:

 echo json_encode(array_values($json));

相同的回复:'没有收到回复'

same response: 'No response received'

我也尝试将 echo $json 放入循环中,我知道这在概念上是错误的,但仍然得到预期的错误语法错误"

I've also tried putting echo $json inside loop which I know that is conceptually wrong but still and got expected error 'Syntax error'

另外,我尝试通过 foreach 回显没有运气语法错误,但我可以看到原始输出但无法验证 json.

Also, i tried echoing through foreach no luck Syntax error but i can see output in raw but cannot validate the json.

仅针对 print_r 上的信息,这是响应:

Just for the info on print_r this is the response:

Array (
 [0] => Array ( 
     [Status] => 1 [NewRecord] => Array ( 
                   [customer_id] => 1241 
                   [firstName] => Katy 
                   [lastName] => Lest
                   [email] => klest@yahoo.com [phone] => 787012425
                                         )
               )
 [1] => Array ( 
      [Status] => 1 [NewRecord] => Array ( 
                    [customer_id] => 1242 
                    [firstName] => Hanah 
                    [lastName] => Morrisn 
                    [email] => road@gmail.com
                    [phone] => 144221275  )
              )
 [2] => Array (
       [Status] => 1 [NewRecord] => Array ( 
                    [customer_id] => 1243 
                    [firstName] => James 
                    [lastName] => McGrath
                    [email] => rosamcgrath@hotmail.com
                    [phone] => 79684312  )
             ) 
)

刚刚找到了一种答案,如果有人可以提供帮助,我仍在寻找原因.我拉的记录数是 150+,所以我一次只尝试了 50 条记录,效果很好.任何人都知道我怎样才能真正为我的数组分配更多内存,以便它只能一次保存所有需要的数据?

Just found a sort of answer to this i am still looking for a reason if anyone can help in that please. The number of Records i was pulling were 150+ so i just tried with 50 records at a time and it worked perfectly. Anyone know how can i actually allocate more memory to my array so that it can hold all the required data at once only ?

我也尝试过提供准确的索引,我认为数组内存不足,但这甚至不起作用:

I have also tried by giving accurate index as well i thought that array goes out of memory but this even not working:

 $json = new SplFixedArray($difference);

非常感谢您的帮助.

推荐答案

陷入黑暗:您的某些数据库行包含非 ASCII 字符(例如 ü、é 等).您的数据库连接设置为 latin1,因此数据不是 UTF-8 编码的.json_encode 需要 UTF-8 编码的数据.如果您获取足够多的行,其中就会有包含此类非 UTF-8 数据的行,并且 json_encode 会失败.如果行数足够少,您就不会碰到那些有问题的行.

Stab into the dark: some of your database rows contain non-ASCII characters (e.g. ü, é and such). Your database connection is set to latin1, so the data is not UTF-8 encoded. json_encode requires UTF-8 encoded data. If you fetch enough rows, there will be rows with such non-UTF-8 data in there, and json_encode fails. With few enough rows you happen to not hit those problematic rows.

通过在 json_encode 之后输出 echo json_last_error_msg(); 来测试这一点.

Test this by outputting echo json_last_error_msg(); after json_encode.

将您的数据库连接设置为 UTF-8. 请在此处查看操作方法:UTF-8 all路过

Set your database connection to UTF-8. See here how to do so: UTF-8 all the way through

当您包含 print_r 时,您的浏览器会抱怨无效 JSON 的原因很简单:因为 PHP 会输出大量不是 JSON 的垃圾,浏览器无法将其解码为 JSON.

The reason why your browser complains about invalid JSON when you include a print_r is simple: because then PHP outputs a lot of garbage which isn't JSON, which the browser can't decode as JSON.

这篇关于json_encode() 方法是否有内存限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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