以更简单的方式合并JSON

时间:2014-08-21 02:34:28

标签: javascript json

我有三个JSON字符串。 JSON-A,JSON-B和JSON-C。我需要合并JSON-A和JSON-B来获取JSON-C并将JSON-C拆分为JSON-A和JSON-B

以下是JSON

JSON-A: - >这些是所有可能答案的问题

{
"content": {
    "section": [
        {
            "questions": [
                {
                    "qText": "Have you or your family receicved medication or treatment for any serious conditions in last 2 years?",
                    "qKey": 152,
                    "qType": [
                        {
                            "aType": "You",
                            "ans": [
                                {
                                    "aKey": "102",
                                    "aText": "Yes"
                                },
                                {
                                    "aKey": "106",
                                    "aText": "No"
                                }
                            ]
                        },
                        {
                            "aType": "Your family",
                            "ans": [
                                {
                                    "aKey": "108",
                                    "aText": "Yes"
                                },
                                {
                                    "aKey": "109",
                                    "aText": "No"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

}

JSON-B: ---->选择答案

{
"qkey": "152",
"ans": [
    {
        "aType": "You",
        "aKey": "102"
    },
    {
        "aType": "Your family",
        "aKey": "106"
    }
]
}

JSON-C - >使用 selectedAnswer

输出
{
"content": {
    "section": [
        {
            "questions": [
                {
                    "qText": "Have you or your family receicved medication or treatment for any serious conditions in last 2 years?",
                    "qKey": 152,
                    "qType": [
                        {
                            "aType": "You",
                            "selectedAnswer": "102",
                            "ans": [
                                {
                                    "aKey": "102",
                                    "aText": "Yes"
                                },
                                {
                                    "aKey": "106",
                                    "aText": "No"
                                }
                            ]
                        },
                        {
                            "aType": "Your family",
                            "selectedAnswer": "109",
                            "ans": [
                                {
                                    "aKey": "108",
                                    "aText": "Yes"
                                },
                                {
                                    "aKey": "109",
                                    "aText": "No"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

}

要获得JSON-C,这就是我对JSON-A和JSON-B

所做的
for (var i = 0;  i < JSONA.content.section.length; i++) {
for (var j = 0; j < JSONA.content.section[i].questions.length; j++) {
    for (var k = 0; k < JSONA.content.section[i].questions[j].qType.length; k++){
        for(var p = 0; p < JSONB.length; p++) {
            if (JSONB[p].qKey == JSONA.content.section[i].questions[j].qKey){
                for (var z = 0; z < JSONB[z].ans.length; z++){
                    for(var m = 0; m < JSONA.content.section[i].questions[j].qType.length; m++) {
                        if (JSONA.contents.section[i].questions[j].qType[m].type == JSONB[p].ans[z].type) {
                            JSONA.content.section[i].questions[j].qType[m].selectedAnswer = JSONB[p].ans[z].aKey;
                        }
                    }
                }
            }
        }

    }
}
}

我的问题是:有更好的方法吗?我有6-7个嵌套循环,这似乎很难理解。我宁愿不使用jquery。提前致谢

3 个答案:

答案 0 :(得分:1)

两件事。考虑在源头或在收到JSONB之后对其进行重构,从而对JSONB进行整形。查找此形状会更容易,并删除一些循环:

{
    "152": {
       "You": "102",
       "Your family": "106"
    }   
}

即使在接收到JSON-B之后将其解析为此形式,也只需要循环一次......而不是每个问题完整地循环它。

其次,请查看Array上的forEach方法,以使其更清晰。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

如果两者都有,并且如果每个问题都能得到答案,那么你最终可能会遇到类似的问题:

JSONA.content.section.forEach(function (section) {
    section.questions.forEach(function (question) {
        question.qType.forEach(function (qtype) {
            qtype.selectedAnswer = JSONB[question.qKey][qtype.aType]
        });
    });
});

答案 1 :(得分:1)

虽然仍有四个循环,但请参阅:http://jsfiddle.net/xh7eLoL0/

qkey = jsonB.qkey;

jsonA.content.section.some(
    function CheckEachSection(section) {
        var found;

        found = section.questions.some(
            function FindMatchingQuestion(quest) {
                if (quest.qKey == qkey) {
                    jsonB.ans.forEach(
                        function ApplyEachAnswer(ans) {
                            quest.qType.some(
                                function FindEachMatch(sub) {
                                    if (sub.aType === ans.aType) {
                                        sub.selectedAnswer = ans.aKey;

                                        return true;
                                    }
                                });
                        });

                    return true;
                }
            });

        return found;
    });

答案 2 :(得分:1)

这有帮助吗?

var section_A = JSONA.content.section;
for(var sA in section_A){
  var questions_A = section_A[sA].questions;
  for(var qA in questions_A){
    var qA = questions_A[qA], qType_A = qA.qType;
    for(var b in JSONB){
      var jb = JSONB[b], jb_ans = jb.ans;
      if(jb.qKey === qA.qKey){
        for(var bA in jb_ans){
          var jbA = jb_ans[bA];
          for(var aT in qType_A){
            var qB = qType_A[aT];
            if(qB.type === jbA.type){
              qB.selectedAnswer = jbA.aKey;
            }
          }
        }
      }
    }
  }
}