如何使用jQuery从JSON获取唯一结果?

时间:2012-12-04 10:21:21

标签: jquery json

  

可能重复:
  Get unique results from JSON array using jQuery

我为体育创建了JSON,看起来像是

 [
    {
        "sportName": "Men's Basketball",
        "path": "mbasket",
        "sportID": 7,
        "parentSportName": "Basketball"
    },
    {
        "sportName": "Women's BasketBall",
        "path": "wcc",
        "sportID": 8,
        "parentSportName": "Basketball"
    },
    {
        "sportName": "Women's Cross Country",
        "path": "wbasket",
        "sportID": 9,
        "parentSportName": "Cross Country"
    },
    {
        "sportName": "Men's Cross Country",
        "path": "mcc",
        "sportID": 10,
        "parentSportName": "Cross Country"
    }
]

我想使用jQuery获取唯一的parentSportName,但是我想知道我的JSON结构是否正确或者我需要做一些修改。我需要重新获取值,因为我正在创建嵌套列表并且我的SLOC正在增加。

1 个答案:

答案 0 :(得分:-2)

这可能适合你:

var myobj;
try{
myobj=[{"sportName":"Men's Basketball","path":"mbasket","sportID":7,"parentSportName":"Basketball"},{"sportName":"Women's BasketBall","path":"wcc","sportID":8,"parentSportName":"Basketball"}];
}

if (myobj==undefined){
//json data has problem.
}