JSON.stringify在控制台中工作,但在代码中不起作用

时间:2013-10-08 20:24:42

标签: json stringify

我完全不知所措。我有一些非常简单的代码,它接受一个对象并将其转换为JSON。

        var vl = JSON.stringify(this.visibleLayers);

这始终返回undefined,即变量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));
    },

1 个答案:

答案 0 :(得分:1)

男孩我觉得自己很蠢!事实证明我在某种程度上错误地输入了visibleLayers。声明我调用的变量是visiblelayers,小写l。进行stringify调用我使用visibleLayers,使用大写字母L.使它们一致地解决所有问题。 D'哦!

相关问题