Jquery 当使用slice解析JSON时,性能是一个问题吗?

Jquery 当使用slice解析JSON时,性能是一个问题吗?,jquery,json,ajax,parsing,slice,Jquery,Json,Ajax,Parsing,Slice,我是JSON领域的新手,在过去的一周里,我对这个主题做了很多研究,关于只显示一定数量的JSON,并在最初加载12个JSON后使用按钮单击来显示X计数。我已经学会了如何用PHP调用API,用PHP调用foreach语句来输出JSON内容,但如果在使用按钮显示下一个数量时性能受到影响,那么我在确定的答案或解释中并没有找到多少运气 我调用的JSON有100多个对象,每个对象最多有20个值(阅读后我对术语的理解),它每小时都会用PHP更新一次。在阅读了如何: $showList=12; $countRe

我是JSON领域的新手,在过去的一周里,我对这个主题做了很多研究,关于只显示一定数量的JSON,并在最初加载12个JSON后使用按钮单击来显示X计数。我已经学会了如何用PHP调用API,用PHP调用foreach语句来输出JSON内容,但如果在使用按钮显示下一个数量时性能受到影响,那么我在确定的答案或解释中并没有找到多少运气

我调用的JSON有100多个对象,每个对象最多有20个值(阅读后我对术语的理解),它每小时都会用PHP更新一次。在阅读了如何:

$showList=12;
$countRecords=0;
foreach($products as$product){
如果($countRecords<$showList){
//更多代码
}
++$countRecords;
}
阅读一些建议在服务器端执行此操作的问题

在参考了关于如何创建按钮单击以加载下一个12计数的几个问题后,我担心使用会影响性能,因为它看起来每次都会遍历整个文件,然后对内容进行切片:

我确实看到了另一种方法,其中添加了一个隐藏类,按钮删除了该类,但这不会影响性能:

使用AJAX的另一种方法:

我确实考虑过修改计数器:


如果我有一个大的JSON文件,那么使用Ajax按钮单击加载更多对象的适当执行方式是什么?如果我有一个大的JSON文件,或者使用
.slice()

就我所知,jQuery中的任何解决方案(或任何客户端)都不会延迟返回或影响性能仍然会在限制之前返回整个JSON,因为这样做的目的是提高性能,所以这是毫无用处的

您需要限制返回的JSON服务器端。您仍然可以使用jQuery为ajax创建响应,但是不要直接调用JSON,而是创建一个限制响应的PHP文件

jQuery ajax.php 如何在ajax.php中获得正确的JSON结果取决于如何获取和/或存储JSON

  • 如果要进行API调用,请查看API具有哪些限制/偏移参数。根据API的不同,您可以使用
    $offset
    &
    $limit
    $from
    &
    $to
    。如果您正在这样做,那么您可以直接通过ajax调用来完成

  • 如果将JSON存储在数据库中,则可以通过MYSQL查询获得结果

  • 如果您只有一个JSON字符串/文件,那么可以像第一个PHP示例一样循环使用它。大概是这样的:

//获取您的POST变量
如果(isset($_POST['limit']))$limit=(int)$_POST['limit'];
if(isset($_POST['offset']))$offset=(int)$_POST['offset'];
//获取您的JSON
if(isset($limit)和isset($offset)){
//限制和偏移
$from=($offset+1)*$limit;
$to=$from+$limit;
//获取现有json
$products=json\U decode($existing\U json,true);
$countRecords=0;
$products_to_return=array();
foreach($products as$product){
//如果在$from之前,请跳到下一个产品
如果($countRecords<$from)继续;
//如果我们追求$to,则结束循环
如果($countRecords>$to)中断;
$products_to_return[]=$product;
++$countRecords;
}
//对新的有限json进行编码
$json=json\u encode($products\u to\u return);
echo$json;
}


注意:这些代码都没有经过测试,很可能充满了错误(太晚了!),但它让您知道该怎么做

100条记录,每个记录有20个键,每个键有20个字符,大约会占用100*20*20=40k的内存。对于今天的计算机来说,这算不了什么。当你开始用10s MB的数据压倒浏览器时,问题就开始了。
$showList = 12;
$countRecords = 0;
foreach($products as $product) {
    if ($countRecords < $showList) {
        // more code
    }
    ++$countRecords;
}
// set initial variables
var limit = 10;
var offset = 0;

// get data on button click
$.('#load-more').on('click', function(){
    getData();
});

function getData() {
    $.ajax({
        url:"ajax.php",
        type: "POST",
        data: { limit: limit, offset: offset },
        success: function(json) {

            // increase offset for next call
            offset++;

            // do something with your JSON
        },
   });
}
// get your POST variables
if( isset($_POST['limit']) ) $limit = (int)$_POST['limit'];
if( isset($_POST['offset']) ) $offset = (int)$_POST['offset'];

// get your JSON
if ( isset($limit) && isset($offset) ) {

    $from = ($offset + 1) * $limit;
    $to   = $from + $limit;

    $json = // get JSON objects $from to $to        

    echo $json;        
}
// get your POST variables
if( isset($_POST['limit']) ) $limit = (int)$_POST['limit'];
if( isset($_POST['offset']) ) $offset = (int)$_POST['offset'];

// get your JSON
if ( isset($limit) && isset($offset) ) {

    // limit & offset
    $from = ($offset + 1) * $limit;
    $to   = $from + $limit;

    // get existing json
    $products = json_decode($existing_json, true);
    $countRecords = 0;
    $products_to_return = array();

    foreach($products as $product) {

        // skip to next product if we are before $from
        if ($countRecords < $from) continue;

        // end the loop if we are after $to
        if ($countRecords > $to) break;

        $products_to_return[] = $product;

        ++$countRecords;
    }

    // encode your new limited json
    $json = json_encode($products_to_return);

    echo $json;        
}