2015-05-14 74 views
3

我正在使用Twitter-Kit/Fabric从twitter api获取响应。它返回以下格式的JSON - https://dev.twitter.com/rest/reference/get/search/tweets在Swift中解析twitter api JSON

我查询 - https://api.twitter.com/1.1/search/tweets.json?q=""&geocode=lat,long,radius&count=15

我需要提取文本,并从JSON坐标绘制在地图上的鸣叫。

我在做什么错了下面的代码 - ?

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)") 
      } 

     } 

我的代码运行到println(parsedResult),因为我得到了twitter api的有效响应。但是,我无法从JSON响应中提取鸣叫文本。

+0

您需要在此之前进行身份验证吗?我收到错误:错误的身份验证数据。 (code 215)}) –

+0

我只想查询$ AAPL的例子。我不想发布或查看用户时间表。我应该可以使用Fabric做到这一点,而无需使用户登录或任何正确的? –

回答

0

有一个与你的代码 使用此代码,它的工作最适合我,如果你正在使用Fabric,您可以创建鸣叫对象设定在某一功能的代码上的按钮操作

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)") 
      } 

     } 
+0

嗨!你能指出我的代码中的错误吗?我需要明白我做错了什么。 – rkmr

+0

要分享并发布推文,您首先必须启动Twitter会话,以便应用程序确认用户已登录 –

+0

我已经这么做了。我有一个匿名会话,发生在代码的不同部分。我得到了twitter api的有效回复,代码运行到println(parsedResult)。然而,它解析了我遇到麻烦的文本和坐标的json。 – rkmr

3

一个小错误使用TWTRTweet方法

+ (NSArray *)tweetsWithJSONArray:(NSArray *)array; 

响应它创建TWTRTweet实例从Twitter API JSON响应阵列的阵列。

NSArray *tweetData = [TWTRTweet tweetsWithJSONArray:responseFromTwitter]; 
[tableview reloadData]; 

您可以使用鸣叫对象的tableview来填充cellForRowAtIndexPath。要返回鸣叫细胞,

- (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; 
} 

编辑

斯威夫特,你可以做:

// 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 
    } 

希望它能帮助!

Reference taken from here

+0

嗨!自从我初学iOS开发并开始使用Swift以来,根本不熟悉Objective C。你能用swift代码的帮助来解释吗? – rkmr

+0

@rkmr我添加了swift的代码。 – NightFury

+0

这可以工作,但功能有限。即,您无法重新加载较旧的推文而无需制作自己的Controller类。我会建议TWTRTimelineViewController。尽管它并不完美,我已经在这里打开了一些反对它的问题:https://twittercommunity.com/t/6-twtrtimelineviewcontroller-issues/51534 –