JSON数据模糊合并 [英] JSON Data Fuzzy merge

查看:104
本文介绍了JSON数据模糊合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的JSON数据

I have a JSON data like this

{
"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需要合并,因为它们的名称紧密匹配

I need to search for fuzzy data and merge. For example InvestmentsDeposits and InvestmentsDeposits$$$d need to be merged because it matches closely in name

需要为此使用javascript

Need to use javascript for this

现在,我可以确保源数据始终在末尾始终存入资金,以便与目标数据合并,而不会在不存入资金的情况下(即InvestmentDeposits)进行合并.

For now I can make sure source data will always have $$$d at the end to merge with the target data without $$$d i.e., InvestmentDeposits.

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

My final merged content should be like this

{
    "array": {

    "InvestmentsDeposits": {

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

        }
    }

对此有什么帮助吗?

到目前为止我已经尝试过的

What I have tried so far

var json0 = {

投资存款":{

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

var json1 =

var json1 =

{

"InvestmentsDeposits $$$ d":{

"InvestmentsDeposits$$$d": {

            "NAME": "Deposits",

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

    };


    // Merge object2 into object1, recursively

$.extend(true,json0,json1);

$.extend( true, json0, json1 );

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

I am able to merge the data if i am able to split the InvestmentDeposits and InvestmentDeposits$$$d in to two distinct JSON objects but how to split and move the $$$d data in to another object? to make the jquery extend work

推荐答案

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

Use Object.keys() to find an object's keys and figure out what data to move over. You can compare the first key with the others to find matches, then remove the keys you just looked at until all of them are gone. Here's an example with a similar object.

    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));

请注意,Object.keys() 需要IE9 + .

这篇关于JSON数据模糊合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆