将jQuery对象转换为JSON

时间:2016-04-04 05:45:54

标签: javascript jquery json

我有一个内部值和方法的jQuery对象。我需要将其转换为JSON。

查看以下对象:

enter image description here

我有self.pgrid.config.columnFields如上图所示。

当我将此对象转换为JSON并将该JSON返回给对象时,它不会返回aggregateFuncformatFunc

请看以下JSON.parse(JSON.stringify(self.pgrid.config.columnFields))的屏幕截图:

enter image description here

我想在这里获得这两种方法。我怎么能得到它们?

生成对象的代码如下:

this.columnFields = (config.columns || []).map(function (fieldconfig) {
    fieldconfig = ensureFieldConfig(fieldconfig);
    return createfield(self, axe.Type.COLUMNS, fieldconfig, getfield(self.allFields, fieldconfig.name));
});

function ensureFieldConfig(obj) {
    if (typeof obj === 'string') {
        return {
            name: self.captionToName(obj)
        };
    }
    return obj;
}

function createfield(rootconfig, axetype, fieldconfig, defaultfieldconfig) {
    var axeconfig;
    var fieldAxeconfig;

    if (defaultfieldconfig) {
        switch (axetype) {
            case axe.Type.ROWS:
                axeconfig = rootconfig.rowSettings;
                fieldAxeconfig = defaultfieldconfig.rowSettings;
                break;
            case axe.Type.COLUMNS:
                axeconfig = rootconfig.columnSettings;
                fieldAxeconfig = defaultfieldconfig.columnSettings;
                break;
            case axe.Type.DATA:
                axeconfig = rootconfig.dataSettings;
                fieldAxeconfig = defaultfieldconfig.dataSettings;
                break;
            default:
                axeconfig = null;
                fieldAxeconfig = null;
                break;
        }
    } else {
        axeconfig = null;
        fieldAxeconfig = null;
    }

    var merged = mergefieldconfigs(fieldconfig, fieldAxeconfig, axeconfig, defaultfieldconfig, rootconfig);

    return new Field({
        name: getpropertyvalue('name', merged.configs, ''),
        caption: getpropertyvalue('caption', merged.configs, ''),
        sort: {
            order: getpropertyvalue('order', merged.sorts, null),
            customfunc: getpropertyvalue('customfunc', merged.sorts, null)
        },
        subTotal: {
            visible: getpropertyvalue('visible', merged.subtotals, true),
            collapsible: getpropertyvalue('collapsible', merged.subtotals, true),
            collapsed: getpropertyvalue('collapsed', merged.subtotals, false) && getpropertyvalue('collapsible', merged.subtotals, true)
        },
        aggregateFuncName: getpropertyvalue('aggregateFuncName', merged.functions, 'sum'),
        aggregateFunc: getpropertyvalue('aggregateFunc', merged.functions, aggregation.sum),
        formatFunc: getpropertyvalue('formatFunc', merged.functions, null)
    }, false);
}

function getpropertyvalue(property, configs, defaultvalue) {
    for (var i = 0; i < configs.length; i++) {
        if (configs[i][property] != null) {
            return configs[i][property];
        }
    }
    return defaultvalue;
}

0 个答案:

没有答案