如何合并两个json对象?

时间:2015-04-27 12:19:24

标签: javascript jquery json

对象1:

var string1 = {
    "jobroleid": "1",
    "technologyid": "1",
    "jobrole": "SOFTWARE DEVELOPER",
    "technology": "DOTNET",
    "yoc": [],
    "degree": [],
    "gender": ["Female"],
    "credit": [],
    "minqp": "6",
    "maxqp": "7"
};

对象2:

var string2={'name':'hai'};

如何合并这两个对象?

预期产出:

[{
    "jobroleid": "1",
    "technologyid": "1",
    "jobrole": "SOFTWARE DEVELOPER",
    "technology": "DOTNET",
    "yoc": [],
    "degree": [],
    "gender": ["Female"],
    "credit": [],
    "minqp": "6",
    "maxqp": "7",
    "name": "hai"
}]

2 个答案:

答案 0 :(得分:6)

您可以使用jQuery。extend方法:

var combined = []; 
combined.push($.extend({},string1,string2));

Demo

答案 1 :(得分:1)

使用此

string1[0] = $.extend({}, string1[0], string2);