Parsing twitter api JSON in Swift
I am using Twitter-Kit / Fabric to get response from twitter api. It returns JSON in the following format - https://dev.twitter.com/rest/reference/get/search/tweets
My request is - https://api.twitter.com/1.1/search/tweets.json?q=""&geocode=lat,long,radius&count=15
I need to extract text and coordinates from JSON in order to draw these tweets on a map.
What am I doing wrong with the following code -?
Twitter.sharedInstance().APIClient.sendTwitterRequest(request) {
(response, data, connectionError) -> Void in
if (connectionError == nil) {
var jsonError : NSError?
let parsedResult =
NSJSONSerialization.JSONObjectWithData(data,
options: nil,
error: &jsonError) as NSDictionary
println(parsedResult)
if let tweets = parsedResult["statuses"] as? NSDictionary {
for tweetsDict in tweets {
let title = tweetsDict["text"] as NSString
println("text: \(title)")
let coordinates = tweetsDict["coordinates"]
println("coordinates: \(coordinates)\n")
}
}else {
println("Error: \(connectionError)")
}
}
My code works up to println (parsedResult) as I am getting valid response from twitter api. However, I am having trouble extracting the tweet text from the JSON response.
source to share
If you are using Fabric
, you can create tweets from the response using the methodTWTRTweet
+ (NSArray *)tweetsWithJSONArray:(NSArray *)array;
It creates an array of instances TWTRTweet
from a JSON response array based on the Twitter API.
NSArray *tweetData = [TWTRTweet tweetsWithJSONArray:responseFromTwitter];
[tableview reloadData];
You can use tweet objects to populate the tableview in cellForRowAtIndexPath
. To get a tweet cell back,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"TweetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
TWTRTweet *tweet = twitterResponse[indexPath.row];
[(TWTRTweetTableViewCell *)cell configureWithTweet:tweet];
return cell;
}
EDIT
In Swift, you can do the following:
// Create an array of tweet model objects from a JSON array
tweetData = TWTRTweet.tweetsWithJSONArray(responseFromTwitter)
tableView.reloadData()
//create Tweet cells
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let tweet = tweetData[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier(tweetTableReuseIdentifier, forIndexPath: indexPath) as TWTRTweetTableViewCell
cell.configureWithTweet(tweet)
return cell
}
Hope it helps!
source to share
Small error occured with code Use this code which works best for me, set this code in a specific function on button action
Twitter.sharedInstance().APIClient.sendTwitterRequest(request) {
(response, data, connectionError) -> Void in
if (connectionError == nil) {
var jsonError : NSError?
let parsedResult =
NSJSONSerialization.JSONObjectWithData(data,
options: nil,
error: &jsonError) as NSDictionary
println(parsedResult)
if let tweets = parsedResult["status"] as? NSDictionary {
println(tweets)
for tweetsDict in tweets {
let title = tweetsDict["text"] as NSString
println("text: \(title)")
let coordinates = tweetsDict["coordinates"]
println("coordinates: \(coordinates)\n")
}
}else {
println("Error: \(connectionError)")
}
}
source to share