Parsing malformed JSON with Javascript

后端 未结 7 1644
抹茶落季
抹茶落季 2020-12-21 11:34

I want to parse this content using Javascript. The data looks like this:

{\"ss\":[[\"Thu\",\"7:00\",\"Final\",,\"BAL\",\"19\",\"ATL\",\"20\",,,\"56808\",,\"PRE

7条回答
  •  生来不讨喜
    2020-12-21 12:03

    I am in a similar position - non javascript expert working on a fun project to familiarize myself with javascript, ajax, and json.

    I took three different steps to handle the problem. I welcome any feedback on improving the solution.

    The first step is to query the nfl site to pull down the scores. Because the source of the json, the nfl site, is different from your site, you will have to work around the javascript security constraints against cross domain querying. I found this stackoverflow link to be a good reference. I used JSONP for the workaround. I used http://whateverorigin.org/ as the indirection site.

    $.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent('http://www.nfl.com/liveupdate/scorestrip/scorestrip.json') + '&callback=?', handleQueryForScoresResult);
    

    As others have pointed out, the nfl site returns invalid json data. The following sample line illustrates the problem:

    ["Sun","4:25","Final",,"TEN","7","MIN","30",,,"55571",,"REG5","2012"],

    Notice the empty array element values (the repeated commas with no data in between). So in my json callback function, I corrected the data by adding empty strings (two double quotes) to repeated commas before calling jquery to parse the json data:

    function handleQueryForScoresResult(data) {
        var jsonStr = data.contents;
        jsonStr = jsonStr.replace(/,,/g, ',"",');
        jsonStr = jsonStr.replace(/,,/g, ',"",');
    
        var scoresData = jQuery.parseJSON(jsonStr).ss;
        .
        .
        .
    }
    

    Lastly, I created GameScores object to encapsulate the json data.

    function GameScore(scoreData) {
        this.scoreData = scoreData;
        scoreData[2] = scoreData[2].toLowerCase();
        scoreData[5] = parseInt(scoreData[5]);
        scoreData[7] = parseInt(scoreData[7]);
    } 
    
    function GameScore_getAwayTeam() { return this.scoreData[4]; }
    function GameScore_getHomeTeam() { return this.scoreData[6]; } 
    function GameScore_isFinal() { return this.scoreData[2]=="final"; }  
    function GameScore_getHomeTeamScore() { return this.scoreData[7]; }
    function GameScore_getAwayTeamScore() { return this.scoreData[5]; }
    function GameScore_doesHomeTeamLead() { return this.scoreData[7]> this.scoreData[5]; }
    function GameScore_doesAwayTeamLead() { return this.scoreData[5]> this.scoreData[7]; }
    function GameScore_getWeekId() { return this.scoreData[12]; }
    
    GameScore.prototype.getHomeTeam = GameScore_getHomeTeam;
    GameScore.prototype.getAwayTeam = GameScore_getAwayTeam;
    GameScore.prototype.isFinal = GameScore_isFinal;
    GameScore.prototype.getHomeTeamScore = GameScore_getHomeTeamScore;
    GameScore.prototype.getAwayTeamScore = GameScore_getAwayTeamScore;
    GameScore.prototype.doesHomeTeamLead = GameScore_doesHomeTeamLead;
    GameScore.prototype.doesAwayTeamLead = GameScore_doesAwayTeamLead;
    GameScore.prototype.getWeekId = GameScore_getWeekId;
    

    I only added a few accessors as I did not need most of the data. Your needs may vary.

提交回复
热议问题