【问题标题】:Is there any memory limit for json_encode() method?json_encode() 方法有内存限制吗?
【发布时间】:2016-06-12 11:14:53
【问题描述】:

我正在尝试回显一个由数组组成的 json 编码数组,但我不知道它不会让我打印那个东西。这是我的代码:

<?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”

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

我尝试过的事情:

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

 echo json_encode(array_values($json));

相同的响应:'未收到响应'

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

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

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

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 条记录,而且效果很好。任何人都知道我如何才能真正为我的数组分配更多内存,以便它可以一次保存所有需要的数据?

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

 $json = new SplFixedArray($difference);

非常感谢您的帮助。

【问题讨论】:

  • echo 之后尝试exit
  • 运气不好,同样的错误'没有收到响应'。
  • 注意:mysql_* 函数已被弃用,它们已从 PHP 7 中删除,当您升级到该版本时,您的代码将停止工作。您不应该使用它们编写新代码,而是使用mysqli_* or PDO
  • @GeraldSchneider 确定我会的,实际上我在很长一段时间后都在研究 php,即使那时我还是初学者,所以只是因为熟悉语法,我正在这样做。

标签: php mysql arrays json memory


【解决方案1】:

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

通过在json_encode 之后输出echo json_last_error_msg(); 进行测试。

将您的数据库连接设置为 UTF-8。 在此处查看如何操作:UTF-8 all the way through

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

【讨论】:

  • 现在情况非常尴尬,如果我用 50 个条目回显 json 编码数组,输出是完美的,但是一旦我尝试超过数组的限制,它就会开始显示消息“未收到响应” '。目前我无法理解。
  • 我试图澄清我的回答中发生了什么......!?还有什么我可以澄清的吗?
  • O o o,我想你已经指出了准确的。我也遇到了同样的错误,让我检查一下。很棒很棒
  • 非常感谢@deceze 指出我正确的方向!!
【解决方案2】:

只需使用 json_decode() 你就会得到你需要的结果..

   $array = json_decode($json, true);
   echo "<pre>"; print_r($array);

Array
(
    [0] => Array
        (
            [Status] => 1
            [NewRecord] => Array
                (
                    [fname] => xyz
                    [lname] => abc
                    [gender] => male
                )

        )

    [1] => Array
        (
            [Status] => 1
            [NewRecord] => Array
                (
                    [fname] => 123
                    [lname] => 456
                    [gender] => male
                )

        )

    [2] => Array
        (
            [Status] => 1
            [NewRecord] => Array
                (
                    [fname] => demo
                    [lname] => vvv
                    [gender] => female
                )

        )

)

【讨论】:

  • 刚刚发现了一些东西,请阅读我问题的最后一段。
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多