0

Possible Duplicate:
Get unique results from JSON array using jQuery

I have created JSON for sports which looks like

 [
    {
        "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"
    }
]

I want to fetch unique parentSportName using jQuery, but what I wanted to know if my JSON structure is correct or I need to do some modifications. I need to re fetch the values as am creating nested list and my SLOC is increasing.

Community
  • 1
  • 1
nickalchemist
  • 2,211
  • 6
  • 31
  • 58
  • Refer to the below link http://stackoverflow.com/questions/6680430/get-unique-results-from-json-array-using-jquery – laxonline Dec 04 '12 at 10:23
  • That particular json isn't valid. Wrap the entire json in [] and it'll pass as a JsonArray with JSON objects inside. You can always lint your json @ http://www.jsonlint.com/ – Henrik Andersson Dec 04 '12 at 10:28
  • @laxonline I checked it. But I am creating a nested list so I need to refetch elements corresponding to parentSportName again. I have restrictions on SLOC. – nickalchemist Dec 04 '12 at 10:28
  • check it with http://jsonlint.com/ for validity – Swarne27 Dec 04 '12 at 10:29

1 Answers1

-2

This might do it for you:

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.
}
Tim Post
  • 33,371
  • 15
  • 110
  • 174