JavaScript:无法从Json文件中获取json元素

时间:2014-08-25 09:37:48

标签: javascript jquery json

我有一个JSON文件,其中存储了我想要使用JavaScript和JQuery动态获取的区域列表。这是我的文件内容的一个例子:



        {"Regions":
            [
                "Region":{
                "type":"polyline",
                "title":"Region 1",
                "strokeColor":"#000000",
                "strokeOpacity":1,
                "strokeWeight":3,
                "path":[{"lat":"21.32008096400822","lng":"79.376220703125"},{"lat":"21.524627220545295","lng":"80.48583984375"},{"lat":"20.488773287109833","lng":"80.2001953125"},{"lat":"20.427012814257385","lng":"79.552001953125"},{"lat":"20.612219573881042","lng":"79.112548828125"},{"lat":"21.32008096400822","lng":"79.376220703125"}]
                },"Region":{
                "type":"polyline",
                "title":"Region 2",
                "strokeColor":"#000000",
                "strokeOpacity":1,
                "strokeWeight":3,
                "path":[{"lat":"21.32008096400822","lng":"79.376220703125"},{"lat":"21.524627220545295","lng":"80.48583984375"},{"lat":"20.488773287109833","lng":"80.2001953125"},{"lat":"20.427012814257385","lng":"79.552001953125"},{"lat":"20.612219573881042","lng":"79.112548828125"},{"lat":"21.32008096400822","lng":"79.376220703125"}]
                }
            ]
    }

这是我的JS代码:



    $.getJSON( "regions.json", function( data ) {
            var regions = [];
            $.each(data,function(key,value){
                    if(key=='Regions'){
                        regions=value;
                        $.each(regions,function(key,value){
                            if(key=='Region'){
                                alert(value);
                            }   
                        });
                    }   
                });
    });

当我测试这段代码时,我只得到一个提示,而我没有得到其他区域。

1 个答案:

答案 0 :(得分:2)

您的JSON数据格式错误,请将“区域”更改为数组:

{
    "Regions": [
        {
            "type": "polyline",
            "title": "Region 1",
            "strokeColor": "#000000",
            "strokeOpacity": 1,
            "strokeWeight": 3,
            "path": [
                {
                    "lat": "21.32008096400822",
                    "lng": "79.376220703125"
                },
                {
                    "lat": "21.524627220545295",
                    "lng": "80.48583984375"
                },
                {
                    "lat": "20.488773287109833",
                    "lng": "80.2001953125"
                },
                {
                    "lat": "20.427012814257385",
                    "lng": "79.552001953125"
                },
                {
                    "lat": "20.612219573881042",
                    "lng": "79.112548828125"
                },
                {
                    "lat": "21.32008096400822",
                    "lng": "79.376220703125"
                }
            ]
        },
        {
            "type": "polyline",
            "title": "Region 2",
            "strokeColor": "#000000",
            "strokeOpacity": 1,
            "strokeWeight": 3,
            "path": [
                {
                    "lat": "21.32008096400822",
                    "lng": "79.376220703125"
                },
                {
                    "lat": "21.524627220545295",
                    "lng": "80.48583984375"
                },
                {
                    "lat": "20.488773287109833",
                    "lng": "80.2001953125"
                },
                {
                    "lat": "20.427012814257385",
                    "lng": "79.552001953125"
                },
                {
                    "lat": "20.612219573881042",
                    "lng": "79.112548828125"
                },
                {
                    "lat": "21.32008096400822",
                    "lng": "79.376220703125"
                }
            ]
        }
    ]
}