2

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

6
  • var jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}]; var jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}]; jsonArray1 = jsonArray1.concat(jsonArray2); Commented Jul 26, 2016 at 4:22
  • My Data as above. this is example that I want Commented Jul 26, 2016 at 4:23
  • That's Javascript, not Swift code Commented Jul 26, 2016 at 4:23
  • so I will get jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}, {'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}]; Commented Jul 26, 2016 at 4:24
  • Yes, I knew it but this is example concept Commented Jul 26, 2016 at 4:25

2 Answers 2

1

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

jsonArray1.extend(jsonArray2) // Swift 1.2
jsonArray1.appendContentsOf(jsonArray2) // Swift 2

In Swift 3.0 you can append like this

jsonArray1.append(contentsOf: jsonArray2)
Sign up to request clarification or add additional context in comments.

Comments

0

parse using SwiftyJSON and concat-

var JSONObject = JSON(json["content"].arrayObject! + json["content"].arrayObject!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.