Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two SwiftyJSON objects

I have a swiftyJSON object that looks like for example:

[{
  "location" : "http://...",
  "img" : "http://...",
  "commentCount" : 0,
  "timestamp" : 1432460217550,
}]

I want to be able to append another swiftyJSON object to it so that it looks like:

[{
  "location" : "http://...",
  "img" : "http://...",
  "commentCount" : 0,
  "timestamp" : 1432460217550,
},
{
  "location" : "http://...",
  "img" : "http://...",
  "commentCount" : 1,
  "timestamp" : 1432460217571,
}
]

I can't use += or .append on swiftyJSON objects. How can I do this?

like image 962
Tometoyou Avatar asked May 24 '15 19:05

Tometoyou


People also ask

How do I merge two JSON objects?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.

Can we merge two JSON files?

This is a simple tool for merging multiple data. json files into one.


1 Answers

As you said, swiftyJSON does not have an append functionality.

What you can do is parse the swiftyJSON objects into an array of type anyObject and append them.

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

Data -> NSData Object received from a HTTP request.

like image 117
Victor Tavares Avatar answered Sep 23 '22 14:09

Victor Tavares