如何将多个json数据合并到jquery的每个循环内的一个对象中

时间:2016-11-28 06:17:10

标签: jquery json

我正在构建一个应用程序,它将接收多个json文件路径作为数组,然后必须使它成为一个单独的对象,我将使用它创建一个数据表。我尝试过一些东西,但没有用。我怎么能实现这个目标呢?

这是我的JSON文件格式

1.json

 {
    "iprsample": [
        {
            "project_id" : "SC.0440",
            "project_name" : "AAA - Testing",
            "review_frequency" : "Monthly",
            "planned_ipr_date" : "2016-02-16T18:30:00Z",
            "actual_ipr_date" : "2016-02-16T18:30:00Z",
            "contract" : "G",
            "finance" : "G",
            "delivery" : "G",
            "people" : "G",
            "process" : "G",
            "project_rag" : "G",
            "isms_compliance" : "G",
            "bcms_compliance" : "G",
            "description" : ""
        }
  ]}

2.json

{
"projects_projectmaster": [
    {
        "id" : 24,
        "project_id" : "SC.0443",
        "project_name" : "AgencyPortfolio Maint (AGS&AGI)",
        "project_start_date" : "2006-12-31T18:30:00Z",
        "sl_head_sbu_head" : "Vidhya R",
        "dh" : "Vidhya R",
        "sbu_head" : "Pramodh Koshy",
        "sl" : "Insurance - LM- Claims",
        "project_category" : "Silver",
        "team_size" : 7,
        "project_cost" : "0.0",
        "project_manager" : "Vyasmurthy Jahagirdar",
        "status" : "Active",
        "customer_name_id" : 24,
        "domain_name_id" : 4,
        "technology" : "24",
        "lifecycle_id" : 7,
        "project_nature_id" : 1,
        "project_owner_id" : 72,
        "sbu_id" : 24
    }
]}

以下是我在Jquery中所做的事情

 /* getting JSON file paths and storing it inside an array */
function filePath(){
    var arr = ['1.json','2.json'];
    return arr;
}

/* merging multiple JSON files in to single */
$(document).ready(function(){
    var arr = filePath();
    var data = [];
    $.each(arr, function(index,value){ 
        $.getJSON(value,function(result){

        });
    });
    return data;
});

我正在努力实现类似

的目标
  var data = {

    "iprsample": [
        {
        "project_id" : "SC.0440",
        "project_name" : "AAA - Testing",
        "review_frequency" : "Monthly",
        "planned_ipr_date" : "2016-02-16T18:30:00Z",
        "actual_ipr_date" : "2016-02-16T18:30:00Z",
        "contract" : "G",
        "finance" : "G",
        "delivery" : "G",
        "people" : "G",
        "process" : "G",
        "project_rag" : "G",
        "isms_compliance" : "G",
        "bcms_compliance" : "G",
        "description" : ""
        }
    ]}


     "projects_projectmaster": [
        {
            "id" : 24,
            "project_id" : "SC.0443",
            "project_name" : "AgencyPortfolio Maint (AGS&AGI)",
            "project_start_date" : "2006-12-31T18:30:00Z",
            "sl_head_sbu_head" : "Vidhya R",
            "dh" : "Vidhya R",
            "sbu_head" : "Pramodh Koshy",
            "sl" : "Insurance - LM- Claims",
            "project_category" : "Silver",
            "team_size" : 7,
            "project_cost" : "0.0",
            "project_manager" : "Vyasmurthy Jahagirdar",
            "status" : "Active",
            "customer_name_id" : 24,
            "domain_name_id" : 4,
            "technology" : "24",
            "lifecycle_id" : 7,
            "project_nature_id" : 1,
            "project_owner_id" : 72,
            "sbu_id" : 24
        }
    ]}
};

提前致谢。

1 个答案:

答案 0 :(得分:0)

<script>
var data1 = {
    "iprsample": [
        {
            "project_id" : "SC.0440",
            "project_name" : "AAA - Testing",
            "review_frequency" : "Monthly",
            "planned_ipr_date" : "2016-02-16T18:30:00Z",
            "actual_ipr_date" : "2016-02-16T18:30:00Z",
            "contract" : "G",
            "finance" : "G",
            "delivery" : "G",
            "people" : "G",
            "process" : "G",
            "project_rag" : "G",
            "isms_compliance" : "G",
            "bcms_compliance" : "G",
            "description" : ""
        }
  ]};
 var data2 = {
"projects_projectmaster": [
    {
        "id" : 24,
        "project_id" : "SC.0443",
        "project_name" : "AgencyPortfolio Maint (AGS&AGI)",
        "project_start_date" : "2006-12-31T18:30:00Z",
        "sl_head_sbu_head" : "Vidhya R",
        "dh" : "Vidhya R",
        "sbu_head" : "Pramodh Koshy",
        "sl" : "Insurance - LM- Claims",
        "project_category" : "Silver",
        "team_size" : 7,
        "project_cost" : "0.0",
        "project_manager" : "Vyasmurthy Jahagirdar",
        "status" : "Active",
        "customer_name_id" : 24,
        "domain_name_id" : 4,
        "technology" : "24",
        "lifecycle_id" : 7,
        "project_nature_id" : 1,
        "project_owner_id" : 72,
        "sbu_id" : 24
    }
]};

function jsonConcat(o1, o2) {
 for (var key in o2) {
  o1[key] = o2[key];
 }
 return o1;
}

var data =  jsonConcat(data1, data2);

console.log(data);
</script>