admin管理员组文章数量:1401241
I want an alternative method for JSON.stringify(). I am using JSON.stringify and I got error like cyclic object value. I don't know how to remove this error so I'd like to know if there's an alternative method.
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;
};
I want an alternative method for JSON.stringify(). I am using JSON.stringify and I got error like cyclic object value. I don't know how to remove this error so I'd like to know if there's an alternative method.
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;
};
Share
Improve this question
edited Dec 19, 2013 at 8:29
JJJ
33.2k20 gold badges94 silver badges103 bronze badges
asked Dec 19, 2013 at 8:12
shubhangi nemadeshubhangi nemade
311 gold badge1 silver badge2 bronze badges
4
- Can you replicate this in a Fiddle or at least include the error message in the question? – David Hellsing Commented Dec 19, 2013 at 8:29
- 2 The error message means that you have a reference to the object itself, which would create an infinite loop when trying to stringify it. Find which key holds the reference, remove it, and you're good to go. – JJJ Commented Dec 19, 2013 at 8:31
- Are you sure that you don't have a cyclic Object graph? – Paul Whelan Commented Dec 19, 2013 at 8:33
-
var arr = JSON.stringify(workspace, null, 4)
you only use this for read the json? But console.log can read a object... and show you the structure... You can useconsole.log(workspace)
and read the response tree in the console... – Frogmouth Commented Dec 19, 2013 at 8:34
2 Answers
Reset to default 3If won't to use stringify you can reproduce it, like this:
OBJtoString(object [,n])
accempt 2 args:
object
(needed) the object you need to log (in my release array and object is the same)n
numbero of space that indent every new line inside object aruments.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; }
Uses Example:
var example = {ciao : "hellow", saluto : {ciao : "ciao", dam : true}};
example.parentObj = example;
console.log(OBJtoString(example,4));
return:
{
ciao : 'hellow'
saluto : {
ciao : 'ciao'
dam : true (boolean)
}
parentObj : FIRST PARENT OBJECT (prevent loop)
}
Other Example:
var example = {ciao : "hellow", saluto : {ciao : "ciao", dam : true}};
example.parentObj = example;
example.f = {
g : example
}
console.log(OBJtoString(example,4));
Return:
{
ciao : 'hellow'
saluto : {
ciao : 'ciao'
dam : true (boolean)
}
parentObj : FIRST PARENT OBJECT (prevent loop)
f : {
g : FIRST PARENT OBJECT (prevent loop)
}
}
I found util.inspect() pretty helpful.
You can print entire object
console.log(util.inspect(myObj))
Or with optional arguments
util.inspect(object, showHidden=false, depth=2, colorize=true)
Documentation: https://nodejs/en/knowledge/getting-started/how-to-use-util-inspect/
本文标签: javascriptAn alternative method for JSONstringify()Stack Overflow
版权声明:本文标题:javascript - An alternative method for JSON.stringify() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744252661a2597323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论