我完全感到困惑。我有一些非常简单的代码,它接受一个对象并将其转换为 JSON。
var vl = JSON.stringify(this.visibleLayers);
这总是返回未定义的,即变量 vl 有一个未定义的值。然而当我检查
JSON.stringify(this.visibleLayers);
在控制台中,它返回我期望的字符串。this.visibleLayers 是一个对象,而不是一个数组。会发生什么?
编辑:添加更多代码 下面是被调用的方法。这是使用 Dojo AMD 风格。基本上,我循环遍历我的地图图层及其子图层,以指示每个图层当前是否在地图中可见。所以我最终得到一个看起来像的对象
{"streets":true,"highways":true,...}
submitExportCustomMap: function() {
domStyle.set("pdfRequestFinished", "display", "none");
domStyle.set("pdfRequestError", "display", "none");
domStyle.set("pdfRequest", "display", "block");
registry.byId("printButton").set("disabled", true);
this.visiblelayers = {};
var _this = this;
array.forEach(map.layerIds, function(layer, i1) {
var ml = map.getLayer(layer);
_this.visiblelayers[ml.id] = ml.visible;
var s = array.forEach(ml.layerInfos, function(sublayer, i2){
if (array.indexOf(ml.visibleLayers, sublayer.id.toString()) !== -1 && ml.visible) {
_this.visiblelayers[sublayer.name] = true;
}
else {
_this.visiblelayers[sublayer.name] = false;
}
});
});
this.prepareCommonParameters();
var vl = JSON.stringify(this.visibleLayers);
var params = {
"xMin": this.extent.xmin,
"yMin": this.extent.ymin,
"xMax": this.extent.xmax,
"yMax": this.extent.ymax,
"Spatial_Reference": this.spatRef,
"Map_Scale": this.mapScale,
"Visiblelayers": vl,
"Layout": this.layout,
"Map_Title": dom.byId("mapTitle").value,
"PointGraphics": graph.getFeatureSet("point"),
"LineGraphics": graph.getFeatureSet("polyline"),
"PolyGraphics": graph.getFeatureSet("polygon")
};
this.cmgp.submitJob(params, lang.hitch(this, this.pdfCompleteCallbackCM), lang.hitch(this, this.pdfStatusCallback), lang.hitch(this, this.pdfErrorCallback));
},