简体   繁体   中英

How to merge two json objects?

Object 1:

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

Object 2:

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

How can I merge the two objects?

Expected output:

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

You can use the jQuery. extend method:

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

Demo

用这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM