如何从Extjs.Class编码到JSON字符串?

Los*_*ode 0 json extjs extjs4

我想将EXTjs类编码为Json,但是,我不能..

我使用JSON.stringify,但这给出了Exception和Type错误.

我怎样才能做到这一点?

谢谢,在这里我的代码.

Ext.define('Text',{
    extend : 'Ext.Img',
    x : 50,
    y : 50,
    size : 100,
    text : 'Text',
    name : 'Text',
    src : ' ',
    tag : '',
    Events : []
});

var text = new Text();
var temp = JSON.stringify(text);
Run Code Online (Sandbox Code Playgroud)

MMT*_*MMT 8

尝试使用

Ext.encode(Object)
Run Code Online (Sandbox Code Playgroud)

它对对象,数组或其他值进行编码并返回JSON字符串.

参考Ext.JSON

序列化对象


use*_*621 5

这里的问题是ExtJS在对象上创建内部引用,结果是周期性的.因此,默认的JSON序列化程序失败.

您需要手动定义一个toJSON将由以下方法调用的方法JSON.stringify:

Ext.define('Text', {
    extend : 'Ext.Img',
    x : 50,
    y : 50,
    size : 100,
    text : 'Text',
    name : 'Text',
    src : ' ',
    tag : '',
    Events : [],

    toJSON: function () {
        return 'Whatever you like' + this.text + this.size // etc.
    }

});

JSON.stringify(new Text()); // "Whatever you likeText100"
Run Code Online (Sandbox Code Playgroud)