JSON仅检索具有特定参数的对象

时间:2015-02-28 10:41:59

标签: jquery ajax json

我已创建此实时更新脚本,每5秒更新一次当前匹配项。但是我想创建一个选项卡,您可以在其中选择不同的运动,因此我需要能够仅检索关键游戏等于足球的对象。如何创建一种函数或额外参数才能实现这一目标?

var lastLoadedMatch = 0,
ajaxInterval = 5000;

$(function() {
  getMatches();

});

function getMatches(lastId) {
$.getJSON('URL JSON', function(resp) {

  var matches = resp.live;
  if (lastId) {
    matches = matches.filter(function(match) {
      return match.id > lastId;
    });
  }

  if (matches.length) {
    $.each(matches, function(_, match) {

      if (match.match_id > lastLoadedMatch) {
        lastLoadedMatch = match.match_id
      }
      $('body').append(matchHtml(this))
    });

  }else{

  }

  setTimeout(function() {
      getMatches(lastLoadedMatch);
    }, ajaxInterval);




});
}


 function matchHtml(obj) {
 var team1 = obj['team 1'],
 team2 = obj['team 2'];
 var html = '<div class="match" data-id="' + obj.match_id + '">';
 html += '<h5>Match</h5>';
 html += '<p>Team 1 = ' + team1.name + ', bet = ' + team1.bet + '<br>';
 html += 'Team 2 = ' + team2.name + ', bet = ' + team2.bet + '</p>';
 html += '</div>';

 return html;

 }

的Json

{
    "live": [
    {
        "match_id": "65551",
        "has_vods": false,
        "game": "hearthstone",
        "team 1": {
            "score": "",
            "name": "ViCi Gaming",
            "bet": "39%"
        },
        "team 2": {
            "score": "",
            "name": "ZhanQi TV",
            "bet": "61%"
        },
        "live in": "Live",
        "title": "ViCi Gaming... 39% vs 61% ZhanQi TV",
        "url": "",
        "tounament": "http://www.gosugamers.net/",
        "simple_title": "ViCi Gaming... vs ZhanQi TV",
        "streams": [
            "http://www.twitch.tv/widgets/live_embed_player.swf?channel=Curemew"
        ]
    },
    {
        "match_id": "64907",
        "has_vods": false,
        "game": "dota2",
        "team 1": {
            "score": "",
            "name": "Summoner's Rift",
            "bet": "19%"
        },
        "team 2": {
            "score": "",
            "name": "Thundercats!",
            "bet": "12%"
        },
        "live in": "Live",
        "title": "Summoner's Rift 19% vs 12% Thundercats!",
        "url": "",
        "tounament": "http://www.gosugamers.net/",
        "simple_title": "Summoner's Rift vs Thundercats!",
        "streams": [
            "http://www.majorleaguegaming.com/player/embed/joinDOTA?autoplay=0"
        ]
    }
    ]
}

1 个答案:

答案 0 :(得分:0)

很抱歉,这只是一个非常快速的例子。希望有更多时间的人可以给你一个扩展的例子。

THE JS CODE

var myOptions = {name: "John", time: "2pm" };
$.post( "proc.php",myOptions    ).done(function( data ) {
    var oResult = $.parseJSON(data);
    alert( "Data Loaded: " + oResult );
});

PHP代码 proc.php

<?php
    $arrayResult = array();

    if (isset($_POST['name'])) {
        $arrayResult['name'] = $_POST['name'];
    }
    if (isset($_POST['time'])) {
        $arrayResult['time'] = $_POST['time'];
    }
    echo json_encode($arrayResult);

?>

通过使用上述逻辑,您可以简单地调整js对象的内容,这将决定服务器端返回给您的内容。

通过使用POST,您可以发送数组或其他类型的数据,因为GET只能用于查询字符串/普通网址字符串。

因为post默认情况下不返回JSON,所以简单解析结果如图所示

思考它 您是否要过滤JS内容或请求的JSON内容。 一个是服务器端,另一个是客户端..

所以取决于你最理想的结果和过滤的数据/时间量。但是无论如何都可以将这个逻辑应用到你的后端。