javascript 获取 OpenLayers GET 请求的 responseText JSON 的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16781357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:58:13  来源:igfitidea点击:

Get element of a responseText JSON of an OpenLayers GET request

javascriptjsongisopenlayersresponsetext

提问by Asma.O

I am trying to use OpenLayers.Request.GETto load data from url in JSON format.

我正在尝试使用OpenLayers.Request.GETJSON 格式从 url 加载数据。

Here's the request (note: url works fine, it brings data in JSON format):

这是请求(注意:url 工作正常,它以 JSON 格式提供数据):

var request = OpenLayers.Request.GET({
    url: "http://localhost:8080/geoserver/wrspc/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=wrspc:layer1&maxFeatures=60&outputFormat=json",
    callback: handler
});

for the handler I tried to get the request.responseTextand show a specific key in the json file like this:

对于处理程序,我尝试获取request.responseText并在 json 文件中显示特定键,如下所示:

var obj;

function handler(request) {
    obj = request.responseText;
    alert (obj.features[0].indicator);
}

and here's my JSON:

这是我的 JSON:

 {"type":"FeatureCollection","features":[{"type":"Feature","id":"titid","geometry":{"type":"MultiPolygon","coordinates":[[[[3694.7863290442,3749.0463695516],[9328.2052648721,3756.61081112875],[3694.18117371807,3861.9059202327],[9340.68659347435,3834.4171230714],[9334.7863290442,3749.0463695516],[3634.7863290442,3839.0463695516]]]]},"geometry_name":"the_geom","properties":{"name1":"asme","number":9130,"indicator":"20","gid":939}}],"crs":{"type":"EPSG","properties":{"code":"2684"}}}

but I get this error: (note the TestPrint.html:506 is the alert line)

但我收到此错误:(注意 TestPrint.html:506 是警报线)

 Uncaught TypeError: Cannot read property '0' of undefined TestPrint.html:506
 GeoExt.form.FormPanel.listeners.actioncomplete TestPrint.html:506
 h.Event.fire ext-all.js:21
 h.Observable.fireEvent ext-all.js:21
 (anonymous function) ext-all.js:21
  h.Event.fire ext-all.js:21
  h.Observable.fireEvent ext-all.js:21
  Ext.form.BasicForm.Ext.extend.afterAction ext-all.js:21
  GeoExt.form.SearchAction.Ext.extend.handleResponse SearchAction.js:147
  OpenLayers.Protocol.WFS.v1.OpenLayers.Class.handleRead OpenLayers.js:843
  (anonymous function) OpenLayers.js:413
  (anonymous function) OpenLayers.js:62
  OpenLayers.Request.runCallbacks OpenLayers.js:509
  d.onreadystatechange OpenLayers.js:508
  b.dispatchEvent OpenLayers.js:751
  c OpenLayers.js:744
  _object.onreadystatechange OpenLayers.js:748

回答by Niccolò Campolungo

You don't parse your response, the function you need to use is JSON.parse:

您不解析您的响应,您需要使用的功能是JSON.parse

function handler(request) {
    //responseText is the raw JSON string, you need to convert it to a JS object
    //use var keyword to define new variables inside your function scope
    var obj = JSON.parse(request.responseText);
    //note that indicator is not a valid features property, you should change it!
    alert(obj.features[0].indicator); //return undefined, change it maybe to .type
}

Your error is given by your attempt to access "all your JSON".featureswhich is undefined(have you ever heard about a string that has featuresproperty(it should be a list)? I don't really think so :P

您的错误是由于您尝试访问“所有您的 JSON”。未定义的功能(您是否听说过具有features属性的字符串(它应该是一个列表)?我真的不这么认为:P

回答by Aragon

you can reach your "indicator" object with following way(it is nested under properties feature):

您可以通过以下方式访问您的“指标”对象(它嵌套在属性功能下):

obj.features[0].properties['indicator']

in such cases you can use online json parserfor understanding your json structure.

在这种情况下,您可以使用在线 json 解析器来了解您的 json 结构。

your json:

你的json:

{

    "type":"FeatureCollection",
    "features":[
        {
            "type":"Feature",
            "id":"titid",
            "geometry":{
                "type":"MultiPolygon",
                "coordinates":[
                    []
                ]
            },
            "geometry_name":"the_geom",
            "properties":{
                "name1":"asme",
                "number":9130,
                "indicator":"20",
                "gid":939
            }
        }
    ],
    "crs":{
        "type":"EPSG",
        "properties":{
            "code":"2684"
        }
    }

}

回答by Asma.O

I figured it out, now it works with the evalfunction,

我想通了,现在它可以与该eval功能一起使用,

obj = eval('(' + request.responseText + ')');

and with alert(obj.features[0].properties['indicator']);.

并与alert(obj.features[0].properties['indicator']);