Lyheang IBell Lyheang IBell - 1 year ago 115
Swift Question

How to Concat JSON objects Swift?

I have two Json Array objects with the same structure and I want to concat them together using Swift. Is there an easy way to do this?

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

jsonArray1 = jsonArray1.concat(jsonArray2);

enter image description here

Answer Source

From your image it is look like that you have two array of [[String: AnyObject]] type, So you can append all the element of jsonArray2 to jsonArray1 like this

First Way

jsonArray1 += jsonArray2 

Second Way


In Swift 3.0 you can append like this

jsonArray1.append(contentsOf: jsonArray2)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download