类别/子类别的嵌套JSON,并使用jQuery获取唯一元素

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

标签: jquery json

我正在为体育和子体育创建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"
}

现在我想在parentSportName 下嵌套sportName,即 越野将包含男子越野和女子越野。我需要在JSON中做些什么改变?

PS:我正在创建一个嵌套列表

1 个答案:

答案 0 :(得分:0)

添加包含嵌套运动数组的顶级运动数组。此示例传递json lint验证器。

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

示例:http://jsfiddle.net/Jrf37/