0

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

这是请求(注意: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
});

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

var obj;

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

这是我的 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"}}}

但我收到此错误:(注意 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
4

3 回答 3

2

您不解析您的响应,您需要使用的功能是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
}

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

于 2013-05-27T23:21:18.273 回答
0

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

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

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

于 2013-05-28T08:37:39.693 回答
0

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

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

在这种情况下,您可以使用在线 json 解析器来了解您的 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"
        }
    }

}
于 2013-05-28T06:47:34.650 回答