从JSON字典中的内部嵌套值读取

read from inner nested values in JSON dictionary

本文关键字:嵌套 读取 内部 JSON 字典      更新时间:2023-09-26

我在读取JSON对象的内部时遇到问题,我不明白为什么。也许你能纠正我,或者给我指明正确的方向?我试图在不特别调用"键"的情况下迭代所有内容。也许这不可能?

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结构

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

如果JSON是正确的,那么最内部的Value不是数组。应该是:

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