JSON数据模糊合并

时间:2014-11-07 17:12:03

标签: javascript json

我有像这样的JSON数据

{
"array": {

"InvestmentsDeposits": {

            "NAME": "Investments & Deposits",
            "PARENT": [
                {
                    "CONTENT_ID": "Promotions",
                    "DISPLAY_ORDER": 3,
                    "PATH": "/Promotions"
                }
            ]
        },
        "InvestmentsDeposits$$$d": {

            "NAME": "Deposits",

            "PARENT": [
             {
                "CONTENT_ID": "NewPromotion",
                 "text" : "newtext"
              }
            ]
    }

    }
    }

我需要搜索模糊数据并合并。例如,需要合并InvestmentsDeposits和InvestmentsDeposits $$$ d,因为它在名称

中紧密匹配

需要使用javascript

现在我可以确保源数据最终总是有$$$ d与目标数据合并而没有$$$ d即投资存款。

我的最终合并内容应该是这样的

{
    "array": {

    "InvestmentsDeposits": {

                "NAME": "Deposits",
                "PARENT": [
                    {
                        "CONTENT_ID": "NewPromotion",
                        "DISPLAY_ORDER": 3,
                        "PATH": "/Promotions"
                         "text": "newtext"
                    }
                ]
            }

        }
    }

对此有何帮助?

到目前为止我尝试了什么

var json0 = {

" InvestmentsDeposits":{

            "NAME": "Investments & Deposits",
            "PARENT": [
                {
                    "CONTENT_ID": "Promotions",
                    "DISPLAY_ORDER": 3,
                    "PATH": "/Promotions"
                }
            ]
            }
            };

var json1 =

{

" InvestmentsDeposits $$$ d":{

            "NAME": "Deposits",

            "PARENT": [
             {
                "CONTENT_ID": "NewPromotion",
                 "text" : "newtext"
              }
            ]
    }

    };


    // Merge object2 into object1, recursively

$ .extend(true,json0,json1);

如果我能够将InvestmentDeposits和InvestmentDeposits $$$ d拆分为两个不同的JSON对象,但是如何将$$$ d数据拆分并移动到另一个对象,我能够合并数据?使jquery扩展工作

1 个答案:

答案 0 :(得分:1)

使用Object.keys()查找对象的密钥并找出要移动的数据。您可以将第一个键与其他键进行比较以查找匹配项,然后删除刚查看过的键,直到所有键都消失为止。这是一个有类似对象的例子。



    var dat = {
        "InvestmentsDeposits": {
            "NAME": "Investments & Deposits",
            "CONTENT_ID": "Promotions",
            "DISPLAY_ORDER": 3,
            "PATH": "/Promotions"
        }, "InvestmentsDeposits$$$d": {
            "NAME": "Deposits",
            "CONTENT_ID": "NewPromotion",
             "text" : "newtext"
        },
        "NotLikeTheOthers": {
            "Um": "Yeah."
        }
    
    };
    var result = {}; // This will be the merged object
    var keys = Object.keys(dat); // Contains keys
    while(keys.length) {
        var i=1;
        for(; i<keys.length; i++) { // Find matches
            if(keys[0] == keys[i] + '$$$d') { // Match type 1
                result[keys[i]] = dat[keys[i]]; // Copy orig
                for(var j in dat[keys[0]]) { // Replace values
                    result[keys[i]][j] = dat[keys[0]][j];
                }
                keys.splice(i,1);
                keys.shift();
                i = 0;
                break;
            } else if(keys[i] == keys[0] + '$$$d') { // Reverse matched
                result[keys[0]] = dat[keys[0]];
                for(var j in dat[keys[i]]) {
                    result[keys[0]][j] = dat[keys[i]][j];
                }
                keys.splice(i,1);
                keys.shift();
                i = 0;
                break;
            }
        }
        if(i > 0) { // Didn't find a match
            result[keys[0]] = dat[keys[0]];
            keys.shift();
        }
    }
    alert(JSON.stringify(result));
&#13;
&#13;
&#13;

请注意Object.keys() requires IE9+