JSON 对象遍历(非数组)

        <script>var value = {
    "china": {
        "hangzhou": { "item": "1" },
        "shanghai": { "item": "2" },
        "chengdu": { "item": "3" }
    },
    "America": {
        "aa": { "item": "1" },
        "bb": { "item": "2" }
    },
    "Spain": {
        "dd": { "item": "1" },
        "ee": { "item": "2" },
        "ff": { "item": "3" }
    }
};
            for (var countryObj in value) {
                document.write(countryObj + ":<br />")
                //没用的for(var cityObj in value.countryObj)
                for (var cityObj in value[countryObj]) {
                    document.write('&nbsp;&nbsp;&nbsp;&nbsp;' + cityObj + "<br />");
                    for (var itemObj in value[countryObj][cityObj]) {
                        document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + itemObj + value[countryObj][cityObj][itemObj] + "<br />")
                    }
                }
            }</script>

你可能感兴趣的:(JSON 对象遍历(非数组))