2

I'd like to use a variable to help me dig into a json object. I know I want to use "r1m1" in this case but I won't necessarily know where that key will appear in the json structure.

json structure...

{
"matchups": {
        "r1m1": {
                "category": "Cat 1",
                "team1": "Team 1",
                "team2": "Team 2"
               },
        "r1m2": {
               "category": "Cat 2",
                "team1": "Team 1",
                "team2": "Team 2"
            }
    }
}

Here I am trying unsuccessfully to use "currentpage" to get at the "r1m1" team name.

var currentPage = "r1m1";

$.getJSON("json.js", function(data) {
    var thisvalue = data.matchups. + currentPage + .team1;
    alert(thisvalue);
}
3
  • what makes you think you need those plus signs? i think you can just get it with data.matchups.r1m1.team1 Commented Mar 8, 2014 at 21:43
  • possible duplicate of Accessing JSON values with a variable Commented Mar 8, 2014 at 21:44
  • 6
    data.matchups[currentPage].team1, Bracket notation. Commented Mar 8, 2014 at 21:44

1 Answer 1

5

Instead of dot notation for the variable, you can use bracket notation

data.matchups[currentPage].team1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.