JSON.stringify()的另一种方法

时间:2013-12-19 08:12:21

标签: javascript

我想要一个JSON.stringify()的替代方法。 我正在使用JSON.stringify,我得到像循环对象值的错误。我不知道如何删除此错误,所以我想知道是否有替代方法。

DCSSPACE.SaveAndOpenJSONWriter = function(canvas) {

    //multiple tab.....
    var mydata = [];
    var area = {}, DCS = {}, workspace = {};
    var len = DCSSPACE.collection.length;
    for (var k = 0; k < len; k++) {
        var result = [];
        var tabid = DCSSPACE.collection.models[k].id;
        var canvas = DCSSPACE.collection.get(tabid).get("workflow");
        //for node
        var figures = canvas.getFigures();
        for (var i = 0; i < figures.getSize(); i++) {
            if (figures.get(i).type == "draw2d.Node") {
                var node = {};

                node.blockType = getBlockType(figures.get(i));
                node.x = figures.get(i).getX();
                node.y = figures.get(i).getY();
                node.id = figures.get(i).getId();
                node.type = figures.get(i).type;
                node.width = figures.get(i).getWidth();
                node.height = figures.get(i).getHeight();
                node.label = figures.get(i).getLabel();
                node.sequenceNo = figures.get(i).getSequenceNo();
            node.model = figures.get(i).model;

                result.push(node);
            }
        }
        //for lines
        var lines = canvas.getLines();
        for (var j = 0; j < lines.getSize(); j++) {
            if (lines.get(j).type == "draw2d.nodeConnetion") {
                var nodeConnection = lines.get(j).getConnectionAttrubutes();

                nodeConnection.source = lines.get(j).getSource();
                nodeConnection.target = lines.get(j).getTarget();
                result.push(nodeConnection);
            }
        }
        area = {
            tabid : tabid,
            attr : result
        };

        mydata.push(area);
        result=[];
        workspace = {
            DCS : mydata
        };
    }
    //console.log(mydata);
    var arr = JSON.stringify(workspace, null, 4);
    console.log(arr);
    DCSSPACE.SaveAndLoadFigure = result;

    return workspace;

};

1 个答案:

答案 0 :(得分:0)

如果不使用stringify,您可以重现它,如下所示:

OBJtoString(object [,n])代表2 args:

  • object(需要)您需要记录的对象(在我的发布数组和对象中是相同的)
  • n缩进对象历史中每个新行的空格数。

    var OBJtoString = function(_o,_m,_rf,_dep,_res){
        _dep = [],
        _rf = function(obj,n,_n,_a,_k,_i,_ws,_f,_e){
            if(typeof obj != "object") return false;
            if(typeof _a === "undefined") _a = "FIRST PARENT OBJECT";
            _dep.push(obj),
            _f = (_dep.length < 1) ? function(){} : function(_z,_x){
                for(_x = 0; _x <= _dep.length; _x++){
                    if(obj[_z] == _dep[_x]) return true;
                }
                return false;
            }
            _ws = "";
            if(typeof n === "undefined") n = 1;
            if(typeof _n === "undefined") _n = n;
            for(_k = 0; _k <= n; _k++){
                _ws += " ";
            }
            var response ="{ \n";
            for(var _i in obj){
                if(typeof obj[_i] !== "object"){
                if(typeof obj[_i] === "string") obj[_i] = "'"+obj[_i].toString()+"'";
                if(typeof obj[_i] === "boolean") obj[_i] = obj[_i].toString() + " (boolean)";
                response += _ws + _i + " : " + obj[_i].toString();
                response += "\n";
                continue;
            }
                response += (_f(_i)) ? _ws + _i + " : "+ _a +" (prevent loop)\n" : _ws + _i + " : " + _rf(obj[_i],n+_n,_n,_a);
            }
        if(_n != n) response += _ws;
        return response +="} \n";
        }
    _res = _rf(_o,_m);
    _dep = [];
    return _res;
     }
    

使用示例:

var example = {ciao : "hellow", saluto : {ciao : "ciao", dam : true}};
example.parentObj = example;
console.log(OBJtoString(example,4));

返回:

 { 
     ciao : 'hellow'
     saluto : { 
          ciao : 'ciao'
          dam : true (boolean)
     }
     parentObj : FIRST PARENT OBJECT (prevent loop)
}

其他示例:

var example = {ciao : "hellow", saluto : {ciao : "ciao", dam : true}};
example.parentObj = example;
example.f = {
    g : example
}
console.log(OBJtoString(example,4));

返回:

{ 
     ciao : 'hellow'
     saluto : { 
         ciao : 'ciao'
         dam : true (boolean)
         } 
     parentObj : FIRST PARENT OBJECT (prevent loop)
     f : { 
         g : FIRST PARENT OBJECT (prevent loop)
         } 
}