Layne
Layne

Reputation: 672

Getting at the data in my json file?

I'm still trying to wrap my head around json and getting access to the data.

Here's my json format, which I hope is logical for the type of data I'm using...

{"round1": [
    {
        "category" : "north",
        "match1": [
            {
                "name": "Team1",
                "score": 90
            },
            {
                "name": "Team2",
                "score": 96
            }
        ]
    },
    {
        "category" : "east",
        "match2": [
            {
                "name": "Team3",
                "score": 32
            },
            {
                "name": "Team4",
                "score": 84
            }
        ]
    }
],
    "round2": [
        {
            "category" : "south",
            "match1": [
                {
                    "name": "Team2",
                    "score": 90
                },
                {
                    "name": "Team4",
                    "score": 96
                }
            ]
        }
    ]
} 

I'm trying to get at the data in there using...

$(document).ready(function(){
    //LOAD JSON
    $.getJSON("json.js", function(data) {
        alert(data.round1[i].match1[i]);
    }
}

How would I get at the following data:

Upvotes: 0

Views: 45

Answers (2)

enginkartal
enginkartal

Reputation: 44

You can do as in this example

$(document).ready(function(){
  //LOAD JSON
  $.each(data,function(i,item){
    console.log(item);
  });
});

Demo at jsfiddle

Upvotes: 1

Matt
Matt

Reputation: 584

JSON is just a JavaScript object. So if your JSON is already an object in JavaScript, you can simply access data like this: object.round1[1].category.

Upvotes: 1

Related Questions