简体   繁体   English

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

[英]JavaScript : can't get json elements from a Json File

I have a JSON file in which is stored a list of regions that I want to get dynamically using JavaScript and JQuery. 我有一个JSON文件,其中存储了要使用JavaScript和JQuery动态获取的区域列表。 here's the an example of the content of my file : 这是我文件内容的示例:



        {"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"}]
                }
            ]
    }

here's my JS code : 这是我的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);
                            }   
                        });
                    }   
                });
    });

When I test this code I get only one prompt , and I don't get the other regions . 当我测试这段代码时,我只会得到一个提示,而我没有得到其他区域。

Your JSON data is in wrong format, change 'Regions' to array: 您的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"
                }
            ]
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM