Michael Meritt
Michael Meritt

Reputation: 393

read from inner nested values in JSON dictionary

I am having trouble reading the inner portions of my JSON object, and I don't understand why. Perhaps you can correct me or point me in the right direction? I am trying to iterate through all of the contents without specifically calling out the 'keys'. Maybe this isn't possible?

JavaScript

console.log("char " + $scope.json.Versions[0].Value[0].Value[0].Character);
console.log("oppo " + $scope.json.Versions[0].Value[0].Value[0].Opponents);


    for(var i in $scope.json.Versions)
    {
        console.log(i);
        for(var j in i)
        {
            console.log(j);
            for(var k in j)
            {
                console.log(k);
            }
        }
    }

JSON Structure

{
    "Versions": [{
        "Key": "1.0.0.0",
        "Value": [{
            "Key": "22",
            "Value": {
                "Character": "22",
                "Opponents": ["20",
                    "0"
                ]
            }
        }, {
            "Key": "18",
            "Value": {
                "Character": "18",
                "Opponents": ["22",
                    "0"
                ]
            }
        }]
    }]
}

Upvotes: 0

Views: 30

Answers (1)

svidgen
svidgen

Reputation: 14282

If your JSON is correct, your inner-most Value isn't an array. It should be:

console.log("char " + $scope.json.Versions[0].Value[0].Value.Character);
console.log("oppo " + $scope.json.Versions[0].Value[0].Value.Opponents);

Upvotes: 1

Related Questions