首页 > 解决方案 > 解析/循环通过此数据并使用 javascript 打印名称和商家

问题描述

aqw= {
    "res": {
        "statiscs":{
            "traffic": {
                "0": {
                    "name": "S0",
                    "bytes": 87
                },
            "1": {
                "name": "S1",
                "bytes": 837
            },
            "3": {
                "state": [
                    {
                        "merchant": "seno",
                        "vendor": "miami"
                    }
                ],
                "name": "S1",
                "bytes": 837
            }
        }
        }
    }
}

console.log('...', aqw.res.statiscs.traffic.0.name);

我尝试使用 console.log('...', aqw.res.statiscs.traffic.0.name) 但我没有定义。我尝试使用角度 ng-repeat 但停留在访问动态数字键

标签: jsonobjectnested-loops

解决方案


您不能使用数字点表示法。为此使用括号。

aqw= {
    "res": {
        "statiscs":{
            "traffic": {
                "0": {
                    "name": "S0",
                    "bytes": 87
                },
            "1": {
                "name": "S1",
                "bytes": 837
            },
            "3": {
                "state": [
                    {
                        "merchant": "seno",
                        "vendor": "miami"
                    }
                ],
                "name": "S1",
                "bytes": 837
            }
        }
        }
    }
}

console.log('...', aqw.res.statiscs.traffic["0"].name);


推荐阅读