KineticJS object with added property to JSON

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:06:53

问题


I am trying to serialize KineticJS object (with one added property) to JSON, but if I call the JSON.stringify(test);, it returns only JSON representation of KineticJS object without my added property. Does anybody know, where could be a problem, please?

 test = new Kinetic.Line(
                {
                    points : [ 29, 30, 31, 32 ],
                    stroke : 'black',
                    strokeWidth : 2,
                    lineJoin : 'round',
                    id : '#line'
                });
 test.obj = "my own property";
 JSON.stringify(test);

returns

{"attrs":{"points":[{"x":29,"y":30},{"x":31,"y":32}],"stroke":"black","strokeWidth":2,"lineJoin":"round","id":"#line"},
"nodeType":"Shape","shapeType":"Line"}"

But I need also information about test.obj..


回答1:


You can do this like that :

var test = new Object();

test.kinetic = new Kinetic.Line(
                {
                    points : [ 29, 30, 31, 32 ],
                    stroke : 'black',
                    strokeWidth : 2,
                    lineJoin : 'round',
                    id : '#line'
                });
 test.obj = "my own property";
 console.log(JSON.stringify(test));


来源:https://stackoverflow.com/questions/16651167/kineticjs-object-with-added-property-to-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!