如何以递归方式修改已解析的JSON对象中的嵌套属性

时间:2018-06-19 11:59:21

标签: javascript arrays json odata traversal

我已经解析了表示来自旧OData(v2)服务的响应的JSON对象:

"TimesheetDetailsSet": {
    "results": [{
        "TimesheetNumber": "1",
        (...)
            "results": [{
                "__metadata":

因此您可以看到它是带有嵌套对象和数组的复杂对象。我使用SAP UI5框架,并且需要对其进行修改,因此将没有称为“结果”的中间数组。我希望“ TimesheetDetailsS​​et”和其他包含“ results”的实体集是独立的对象数组。

我写了这个方法:

var fnConvert = function (oData) {

    if (!oData) {
        return oData;
    }

    for (var sKey in oData) {
        if (oData.hasOwnProperty(sKey)) {
            if (sKey == 'results') {
                var oArr = oData[sKey].reduce(function (acc, cur, i) {
                    acc[i] = cur;
                    return acc;
                }, []);

                delete oData[sKey]
                for (var i = 0; i < oArr.length; i++) {
                    oData[i] = oArr[i];
                    fnConvert(oData[i]);
                }

            } else if (typeof oData[sKey] === 'object') {
                fnConvert(oData[sKey]);
            }
        }
    }
    return oData;
};

唯一的问题是需要实体集是数组而不是对象。

Result of the method

更新:

我明白了:

"TimesheetDetailsSet": {
    "results": [{
        "__metadata": {
            "id": "...",
            "uri": "...",
            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.TimesheetDetails"
        },
        "TimesheetNumber": "1",
        "Username": "DEFAULT_USER",
        "TimesheetKey": "\/Date(1529280000000)\/",
        "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
        "CostObject": "0001",
        "ConstructionSiteSet": {
            "results": [{
                "__metadata": {
                    "id": "...",
                    "uri": "...",
                    "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSite"
                },
                "TimesheetKey": "\/Date(1529280000000)\/",
                "WorkingOnConstrSiteSince": "18.08.2018",
                "Username": "DEFAULT_USER",
                "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
                "ConstructionSiteKey": "00000100-0000-0000-0000-000000000000",
                "ConstructionSiteId": "0001",
                "ConstructionSiteDetailSet": {
                    "results": [{
                        "__metadata": {
                            "id": "...",
                            "uri": "...",
                            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSiteDetail"
                        },

我想将其更改为此:

"TimesheetDetailsSet": [{
        "__metadata": {
            "id": "...",
            "uri": "...",
            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.TimesheetDetails"
        },
        "TimesheetNumber": "1",
        "Username": "DEFAULT_USER",
        "TimesheetKey": "\/Date(1529280000000)\/",
        "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
        "CostObject": "0001",
        "ConstructionSiteSet": [{
                "__metadata": {
                    "id": "...",
                    "uri": "...",
                    "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSite"
                },
                "TimesheetKey": "\/Date(1529280000000)\/",
                "WorkingOnConstrSiteSince": "18.08.2018",
                "Username": "DEFAULT_USER",
                "TimesheetDetailKey": "00000001-0000-0000-0000-000000000000",
                "ConstructionSiteKey": "00000100-0000-0000-0000-000000000000",
                "ConstructionSiteId": "0001",
                "ConstructionSiteDetailSet": [{
                        "__metadata": {
                            "id": "...",
                            "uri": "...",
                            "type": "ZHR_XSS_ASA_ESS_ODATA_SRV.ConstructionSiteDetail"
                        },

0 个答案:

没有答案