连接两个JSON对象

时间:2009-01-11 20:29:55

标签: javascript json

我有两个具有相同结构的JSON对象,我想使用Javascript将它们连接在一起。有一个简单的方法吗?

13 个答案:

答案 0 :(得分:82)

根据您在评论中的描述,您只需执行数组连接:

var jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}];
var jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}];
jsonArray1 = jsonArray1.concat(jsonArray2);
// jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}, 
//{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}];

答案 1 :(得分:34)

如果您要复制属性:

var json1 = { value1: '1', value2: '2' };
var json2 = { value2: '4', value3: '3' };


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

var output = {};
output = jsonConcat(output, json1);
output = jsonConcat(output, json2);

上述代码的输出为{ value1: '1', value2: '4', value3: '3' }

答案 2 :(得分:20)

您可以使用jquery extend方法。

示例:

o1 = {"foo":"bar", "data":{"id":"1"}};
o2 = {"x":"y"};
sum = $.extend(o1, o2);

结果:

sum = {"foo":"bar", "data":{"id":"1"}, "x":"y"}

答案 3 :(得分:14)

一种解决方案是使用列表/数组:

var first_json = {"name":"joe", "age":27};
var second_json = {"name":"james", "age":32};

var jsons = new Array();
jsons.push(first_json);
jsons.push(second_json);

<强>结果

jsons = [
    {"name":"joe", "age":27},
    {"name":"james", "age":32}
]

答案 4 :(得分:14)

实际的方法是使用JS Object.assign。

Object.assign(target, ...sources)

MDN Link

还有另一个针对ES7提出的对象扩展运算符,可以与Babel插件一起使用。

 Obj = {...sourceObj1, ...sourceObj2}

答案 5 :(得分:2)

您可以使用 Object.assign()方法。 Object.assign()方法用于将所有可枚举的自身属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。[1]

var o1 = { a: 1 }, o2 = { b: 2 }, o3 = { c: 3 };

var obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }

答案 6 :(得分:1)

好的,您可以在一行代码中执行此操作。你需要json2.js(你可能已经有了。)。这里的两个json对象是未解析的字符串。

json1 = '[{"foo":"bar"},{"bar":"foo"},{"name":"craig"}]';

json2 = '[{"foo":"baz"},{"bar":"fob"},{"name":"george"}]';

concattedjson = JSON.stringify(JSON.parse(json1).concat(JSON.parse(json2)));

答案 7 :(得分:1)

试试这个,使用下划线

var json1 = [{ value1: '1', value2: '2' },{ value1: '3', value2: '4' }];
var json2 = [{ value3: 'a', value4: 'b' },{ value3: 'c', value4: 'd' }];
var resultArray = [];
json1.forEach(function(obj, index){
  resultArray.push(_.extend(obj,  json2[index]));
});

console.log("Result Array", resultArray);

结果

答案 8 :(得分:1)

我用

让x = {a:1,b:2,c:3}

让y = {c:4,d:5,e:6}

让z = Object.assign(x,y)

console.log(z)

//输出:

{a:1,b:2,c:4,d:5,e:6}

https://www.quora.com/How-can-I-add-two-JSON-objects-into-one-object-JavaScript

答案 9 :(得分:1)

如果使用TypeScript,则可以使用传播运算符(String line = 'DP|1011274|NC|SE059|20190615|75503515|1|20190529|20190531|36900|3145|NOK|GBP|1|20190619|3145|3999|482171536||482171536|1A|1|39593|329464|475|3954||' ​String [] separated = line.split('\\|') println ​separated.size()​

...

答案 10 :(得分:0)

var baseArrayOfJsonObjects = [{},{}];
for (var i=0; i<arrayOfJsonObjectsFromAjax.length; i++) {
    baseArrayOfJsonObjects.push(arrayOfJsonObjectsFromAjax[i]);
}

答案 11 :(得分:0)

只有Javascript =)

var objA = { 'a': 1, 'b': 2}
var objB = { 'x': 3, 'y': 5}
objA.nameNewObj = objB

enter image description here

答案 12 :(得分:0)

我使用:

let jsonFile = {};    
let schemaJson = {};    
schemaJson["properties"] = {};    
schemaJson["properties"]["key"] = "value";
jsonFile.concat(schemaJson);
相关问题