检查json并仅显示可用数据

时间:2016-01-25 10:04:16

标签: javascript jquery json

我正在开发一个使用json获取数据的简单网站。

代码如下所示:

指数:

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Linköping</title>
    <link type="text/css" rel="stylesheet" media="all" href="css/main.css" />
    <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="js/textmetrics.js"></script>
    <script type="text/javascript">
    var config = {
        // General config
        slideTime: 15*1000,
        fadeTime: 3*1000,


        // Time
        timeUpdate: 60*1000,

        // Names
        nameDayUrl: 'namedays.json',
        nameUpdate : 60*1000,

        // Events
        eventUrl: 'Idrottsevent.json',
        eventUpdate : 1*1000,

        // Weather
        weatherUrl: 'http://api.openweathermap.org/data/2.5/find?lat=58.41&lon=15.62&cnt=1&units=metric&appid=2de143494c0b295cca9337e1e96b00e0',
        weatherUpdate: 1800*1000
    }
    </script>


<div class="slide304" id="eventSlide">
    <div class="center">
        <div id="gameTicker1"></div>
    </div>
</div>
<div class="slide304" id="eventSlide">
    <div class="center">
        <div id="gameTicker2"></div>
    </div>
</div>
<div class="slide304">
    <div class="center">
        <div id="weatherTicker"></div>
    </div>
</div>  

<script type="text/javascript">
    // Global data 
    var _names = {};
    var _events = {};
    var _games = {};

$(document).ready(function() {
        // Get events
        $.ajax({
            url: config.eventUrl,
            async: true,
            cache: false,
            method: 'GET',
            success: function(result) {
                _games = result;
                setGame1();
                setGame2();

            },
            error: function(xhr, status, error) {
                _games = {};
            }
        });

function setGame1() {
        // Select game.
        var nowDate = getTimeForEvents();
        var mm = addZero(nowDate.getMonth() + 1);
        var d = addZero(nowDate.getDate());
        var key = mm + "-" + d + "A";
        var game = (typeof _games[key] != "undefined") ? _games[key] : "";
        $('#gameTicker1').html((game == '') ? '' : game);
        setFontSizeForCurrentSlide();
        setTimeout(setGame1, config.eventUpdate);
    }

        function setGame2() {
        // Select game.
        var nowDate = getTimeForEvents();
        var mm = addZero(nowDate.getMonth() + 1);
        var d = addZero(nowDate.getDate());
        var key = mm + "-" + d + "B";
        var game = (typeof _games[key] != "undefined") ? _games[key] : "";
        $('#gameTicker2').html((game == '') ? '' : game);
        setFontSizeForCurrentSlide();
        setTimeout(setGame2, config.eventUpdate);
    }

这是json代码的一部分。

  "01-25A": "Dagens match:<br />TEST Linköpings VC-Hylte Halmstad<br />15:00 Linköpings sporthall",

代码检查日期和月份以及当天附加到每个游戏的字母。如果一天有两场比赛,则第一场比赛有“A”,第二场比赛有“B”等。

现在,网站会浏览JavaScript中的每个游戏(SetGame1,SetGame2)。在显示第一个游戏后显示一个黑暗的幻灯片,因为json上只有一个游戏。我想要它做的是检查并确保它只滑过可用的游戏并跳过json上没有的游戏。如果json上只有一个游戏,则不应该尝试显示GameSet2。

任何人都可以帮我吗?

更新 完成Json:

{
 "12-27": [
                              "Dagens match:<br />KFUM Linköping-Falcao FC Stockholm<br />13:00 Linköpings Sporthall"                                                       
 ],


 "12-28": [
                              "Dagens match:<br />LHC-Rögle BK<br />19:00 SAAB ARENA"
 ],


 "12-30": [
                              "Dagens match:<br />LHC-Örebro Hockey<br />19:00 SAAB ARENA"                             ],



 "01-02": [
                              "Dagens match:<br />Linköping Innebandy-Malmö FBC<br />14:10 SAAB ARENA",
"Dagens match:<br />Linköping Innebandy-Växjö IBK<br />17:10 SAAB ARENA"
 ],



 "01-08": [
                              "Dagens match:<br />KFUM Linköping- Spartak Örebro Futsal Club<br />20:00 Linköpings Sporthall"
 ],



 "01-10": [
                              "Dagens match:<br />Linköping Innebandy-Pixbo Wallenstam IBK<br />13:00 Folkungahallen",
                              "Dagens match:<br />Linköping Innebandy- FC Helsingborg<br />17:00 Linköpings Sporthall"

 ],

 "01-13": [
                              "Dagens match:<br />Linköping Innebandy-IK Sirius IBK<br />19:00 Linköpings Sporthall"
 ],


 "01-14": [
                              "Dagens match:<br />LHC-Färjestads BK<br />19:00 SAAB ARENA"
 ],


 "01-16": [
                              "Dagens match:<br />Linköpings VC-Örkeljunga<br />15:00 Linköpings Sporthall"
 ],


 "01-17": [
                              "Dagens match:<br />Linköping Innebandy-IBF Falun<br />13:00 Folkungahallen",
                              "Dagens match:<br />RP IF-GF Kroppskultur<br />13:30 Linköpings Sporthall",
                              "Dagens match:<br />RP IF Linköping-HK Silwing/Troja<br />16:00 Linköpings sporthall"
 ],

 "01-23": [
                              "Dagens match:<br />Linköping VC-KFUM Eskilstuna volley<br />15:00 Folkungahallen",
                              "Dagens match:<br />LHC-Frölunda HC<br />16:00 SAAB ARENA"
 ],


 "01-24": [
                              "Dagens match:<br />Linköpings VC-Hylte Halmstad<br />15:00 Linköpings sporthall"
 ],



 "01-25A":                     "Dagens match:<br />TEST Linköpings VC-Hylte Halmstad<br />15:00 Linköpings sporthall",



"01-27": [
                              "Dagens match:<br />Linköpings VC-Södertelge<br />19:00 Linköpings sporthall"
                                                        ],



"01-29": [
                                                          "Dagens match:<br />Linköping Innebandy-Endre IF<br />19:30 Folkungahallen",
                                                          "Dagens match:<br />Linköping HC-MODO Hockey<br />19:00 Stångebro Ishall"

                             ],

"01-30": [
                                                          "Dagens match:<br />LHC-HV71<br />18:30:SAAB ARENA",
                                                          "Dagens match:<br />Linköping VC-Sollentuna<br />13:00 Folkungahallen",
                                                          "Dagens match:<br />Linköping HC-Brynäs IF<br />16:00 Stångebro Ishall",
                            "Dagens match:<br />Linköping Innebandy-Warberg IC<br />15:00 Linköpings Sporthall"
                             ],


"01-31": [
                                                          "Dagens match:<br />Linköping HC-IF Sundsvall Hockey<br />12:00 Stångebro Ishall",
                                                          "Dagens match:<br />RP IF Linköping-Tyresö Handboll<br />13:30 Linköpings sporthall",
                                                          "Dagens match:<br />RP IF Linköping-Brännans HF<br />16:00 Linköpings sporthall"
                             ],



"02-04": [
                                                          "Dagens match:<br />LHC-MODO Hockey<br />19:00 SAAB ARENA"
                             ],


"02-13": [
                                                          "Dagens match:<br />RP IF Linköping-Lidingö SK<br />13:30 Linköpings sporthall",
                                                          "Dagens match:<br />RP IF Linköping-HK GP<br />16:00 Linköpings sporthall",
                                                          "Dagens match:<br />Linköping Innebandy-IBK Dalen<br />14:30 Motala Sporthall"
                             ],

"02-16": [
                                                          "Dagens match:<br />LHC-Skellefteå AIK<br />19:00 SAAB ARENA"
                             ],

"02-19": [
                                                          "Dagens match:<br />Linköping Innebandy-Pixbo Wallenstam IBK<br />19:00 Linköpings Sporthall"
                             ],

 "02-20": [
                                                          "Dagens match:<br />LHC-Växjö Lakers<br />16:00 SAAB ARENA",
                                                          "Dagens match:<br />Linköpings VC-Västerås VBK<br />15:00 Folkungahallen",
                                                          "Dagens match:<br />Linköping HC-SDE HF<br />12:00 Stångebro Ishall"
                             ],

 "02-21": [
                                                          "Dagens match:<br />Linköping HC-Leksands IF<br />13:00 SAAB ARENA",
                                                          "Dagens match:<br />Linköping Innebandy-Rönnby Västerås IBK<br />13:00 Folkungahallen"
                             ],




 "02-24": [
                                                          "Dagens match:<br />Linköpings VC-Sollentuna<br />19:00 Linköpings sporthall",
                                                          "Dagens match:<br />LHC-Rögle BK<br />19:00 SAAB ARENA"
                             ],

 "02-26": [
                                                          "Dagens match:<br />Linköping Innebandy-Huddinge IK<br />19:30 Folkungahallen"
                             ],

"02-27": [
                                                          "Dagens match:<br />Linköpings VC-Tierp<br />15:00 Linköpings sporthall",
                                                          "Dagens match:<br />RP IF Linköping-Skara HF<br />13:30 Linköpings sporthall",
                                                          "Dagens match:<br />LHC-Brynäs IF<br />18:30 SAAB ARENA",
                            "Dagens match:<br />RP IF Linköping-Mantorps IF HF<br />16:00 Linköpings sporthall"
                             ],


 "03-01": [
                                                          "Dagens match:<br />LHC-Örebro Hockey<br />19:00 SAAB ARENA"
                             ],


"03-05": [
                                                          "Dagens match:<br />Linköpings VC-Jomala IK<br />15:00 Folkungahallen",
                                                          "Dagens match:<br />LHC-HV71<br />16:00 SAAB ARENA",
                                                          "Dagens match:<br />Linköping Innebandy-IBK Dalen<br />17:15 Linköpings Sporthall"
                             ],



"03-08": [
                                                          "Dagens match:<br />LHC-Malmö Redhawks<br />19:00 SAAB ARENA"
                             ],


"03-09": [
                                                          "Dagens match:<br />Linköpings VC-Vingåker<br />19:00 Linköpings sporthall"
                             ],


"03-19": [
                                                          "Dagens match:<br />Linköpings VC-Rissne IF<br />15:00 Folkungahallen",
                                                          "Dagens match:<br />RP IF Linköping-Skånela IF<br />17:00 Linköpings sporthall"
                             ],



"03-20": [
                                                          "Dagens match:<br />RP IF Linköping-Enköpings HF<br />15:00 Linköpings sporthall"
                             ]
}

1 个答案:

答案 0 :(得分:0)

使用Object.prototype.hasOwnProperty()

_games = result;

if(_games.hasOwnProperty("game1")) // assuming game1 is the key for game1, replace it with the actual key.
  setGame1();

if(_games.hasOwnProperty("game2"))
  setGame2();