2014-09-17 2 views
0

Newtonsoft의 Json.net 클라이언트로 Twitter API를 구문 분석하려고합니다. 내가 트위터 데이터와 트위터 셀을 연결하려고 할 때하지만 난 문제가 무엇입니까 :JObject to TableView to C#

TableSource.cs을

public JObject twitterData { get; set; } 

if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) { 
     BTProgressHUD.Show ("Getting more..."); 
     FLDTRequest.getInstagramNextPage (tableView); 
     BTProgressHUD.Dismiss(); 
    } 

    var cell = tableView.DequeueReusableCell (TwitterCell.Key) as TwitterCell; 

    if (cell == null) { 
     cell = new TwitterCell(); 
     var views = NSBundle.MainBundle.LoadNib ("TwitterCell", cell, null); 
     cell = Runtime.GetNSObject (views.ValueAt (0)) as TwitterCell; 
    } 

    JObject tweet = twitterData[indexPath.Row]; //Error Here: Cannot convert JToken to JObject 

    cell.BindData (tweet); 
    return cell; 

TwitterRequest.cs

IEnumerable<Account> accounts = AccountStore.Create().FindAccountsForService ("Twitter"); 


var enumerable = accounts as IList<Account> ?? accounts.ToList(); 
if (enumerable.Any()) { 
Account twitter = enumerable.First(); 
var twitterOauthToken = twitter.Properties ["oauth_token"].ToString(); 
var twitterOauthTokenSecret = twitter.Properties ["oauth_token_secret"].ToString(); 
var twitterOauthConsumerKey = twitter.Properties ["oauth_consumer_key"].ToString(); 
var twitterOauthConsumerSecret = twitter.Properties ["oauth_consumer_secret"].ToString(); 

var request = new RestRequest("1.1/statuses/home_timeline.json"); 

var client = new RestClient("https://api.twitter.com") 
{ 
    Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource(twitterOauthConsumerKey, twitterOauthConsumerSecret, twitterOauthToken, twitterOauthTokenSecret) 
}; 
client.ExecuteAsync (request, response => { 
     JObject obj = JsonConvert.DeserializeObject<JObject>(response.Content); 
     Console.WriteLine((string)obj["text"]); 

      ((TableSource)table.Source).twitterData = rootObject.data; 
     }); 
} 

어떤 것 UITableView에 트위터 트윗을 표시하는 올바른 방법?

편집 : //

[ 
    { 
    "coordinates": null, 
    "truncated": false, 
    "created_at": "Tue Aug 28 21:16:23 +0000 2012", 
    "favorited": false, 
    "id_str": "240558470661799936", 
    "in_reply_to_user_id_str": null, 
    "entities": { 
     "urls": [ 

     ], 
     "hashtags": [ 

     ], 
     "user_mentions": [ 

     ] 
    }, 
    "text": "just another test", 
    "contributors": null, 
    "id": 240558470661799936, 
    "retweet_count": 0, 
    "in_reply_to_status_id_str": null, 
    "geo": null, 
    "retweeted": false, 
    "in_reply_to_user_id": null, 
    "place": null, 
    "source": "<a href="//realitytechnicians.com\"" rel="\"nofollow\"">OAuth Dancer Reborn</a>", 
    "user": { 
     "name": "OAuth Dancer", 
     "profile_sidebar_fill_color": "DDEEF6", 
     "profile_background_tile": true, 
     "profile_sidebar_border_color": "C0DEED", 
     "profile_image_url": "http://a0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg", 
     "created_at": "Wed Mar 03 19:37:35 +0000 2010", 
     "location": "San Francisco, CA", 
     "follow_request_sent": false, 
     "id_str": "119476949", 
     "is_translator": false, 
     "profile_link_color": "0084B4", 
     "entities": { 
     "url": { 
      "urls": [ 
      { 
       "expanded_url": null, 
       "url": "http://bit.ly/oauth-dancer", 
       "indices": [ 
       0, 
       26 
       ], 
       "display_url": null 
      } 
      ] 
     }, 
     "description": null 
     }, 
     "default_profile": false, 
     "url": "http://bit.ly/oauth-dancer", 
     "contributors_enabled": false, 
     "favourites_count": 7, 
     "utc_offset": null, 
     "profile_image_url_https": "https://si0.twimg.com/profile_images/730275945/oauth-dancer_normal.jpg", 
     "id": 119476949, 
     "listed_count": 1, 
     "profile_use_background_image": true, 
     "profile_text_color": "333333", 
     "followers_count": 28, 
     "lang": "en", 
     "protected": false, 
     "geo_enabled": true, 
     "notifications": false, 
     "description": "", 
     "profile_background_color": "C0DEED", 
     "verified": false, 
     "time_zone": null, 
     "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/80151733/oauth-dance.png", 
     "statuses_count": 166, 
     "profile_background_image_url": "http://a0.twimg.com/profile_background_images/80151733/oauth-dance.png", 
     "default_profile_image": false, 
     "friends_count": 14, 
     "following": false, 
     "show_all_inline_media": false, 
     "screen_name": "oauth_dancer" 
    }, 
    "in_reply_to_screen_name": null, 
    "in_reply_to_status_id": null 
    }, 
    { 
    "coordinates": { 
     "coordinates": [ 
     -122.25831, 
     37.871609 
     ], 
     "type": "Point" 
    }, 
    "truncated": false, 
    "created_at": "Tue Aug 28 21:08:15 +0000 2012", 
    "favorited": false, 
    "id_str": "240556426106372096", 
    "in_reply_to_user_id_str": null, 
    "entities": { 
     "urls": [ 
     { 
      "expanded_url": "http://blogs.ischool.berkeley.edu/i290-abdt-s12/", 
      "url": "http://t.co/bfj7zkDJ", 
      "indices": [ 
      79, 
      99 
      ], 
      "display_url": "blogs.ischool.berkeley.edu/i290-abdt-s12/" 
     } 
     ], 
     "hashtags": [ 

     ], 
     "user_mentions": [ 
     { 
      "name": "Cal", 
      "id_str": "17445752", 
      "id": 17445752, 
      "indices": [ 
      60, 
      64 
      ], 
      "screen_name": "Cal" 
     }, 
     { 
      "name": "Othman Laraki", 
      "id_str": "20495814", 
      "id": 20495814, 
      "indices": [ 
      70, 
      77 
      ], 
      "screen_name": "othman" 
     } 
     ] 
    }, 
    "text": "lecturing at the \"analyzing big data with twitter\" class at @cal with @othman http://t.co/bfj7zkDJ", 
    "contributors": null, 
    "id": 240556426106372096, 
    "retweet_count": 3, 
    "in_reply_to_status_id_str": null, 
    "geo": { 
     "coordinates": [ 
     37.871609, 
     -122.25831 
     ], 
     "type": "Point" 
    }, 
    "retweeted": false, 
    "possibly_sensitive": false, 
    "in_reply_to_user_id": null, 
    "place": { 
     "name": "Berkeley", 
     "country_code": "US", 
     "country": "United States", 
     "attributes": { 
     }, 
     "url": "http://api.twitter.com/1/geo/id/5ef5b7f391e30aff.json", 
     "id": "5ef5b7f391e30aff", 
     "bounding_box": { 
     "coordinates": [ 
      [ 
      [ 
       -122.367781, 
       37.835727 
      ], 
      [ 
       -122.234185, 
       37.835727 
      ], 
      [ 
       -122.234185, 
       37.905824 
      ], 
      [ 
       -122.367781, 
       37.905824 
      ] 
      ] 
     ], 
     "type": "Polygon" 
     }, 
     "full_name": "Berkeley, CA", 
     "place_type": "city" 
    }, 
    "source": "<a href="//www.apple.com\"" rel="\"nofollow\"">Safari on iOS</a>", 
    "user": { 
     "name": "Raffi Krikorian", 
     "profile_sidebar_fill_color": "DDEEF6", 
     "profile_background_tile": false, 
     "profile_sidebar_border_color": "C0DEED", 
     "profile_image_url": "http://a0.twimg.com/profile_images/1270234259/raffi-headshot-casual_normal.png", 
     "created_at": "Sun Aug 19 14:24:06 +0000 2007", 
     "location": "San Francisco, California", 
     "follow_request_sent": false, 
     "id_str": "8285392", 
     "is_translator": false, 
     "profile_link_color": "0084B4", 
     "entities": { 
     "url": { 
      "urls": [ 
      { 
       "expanded_url": "http://about.me/raffi.krikorian", 
       "url": "http://t.co/eNmnM6q", 
       "indices": [ 
       0, 
       19 
       ], 
       "display_url": "about.me/raffi.krikorian" 
      } 
      ] 
     }, 
     "description": { 
      "urls": [ 

      ] 
     } 
     }, 
     "default_profile": true, 
     "url": "http://t.co/eNmnM6q", 
     "contributors_enabled": false, 
     "favourites_count": 724, 
     "utc_offset": -28800, 
     "profile_image_url_https": "https://si0.twimg.com/profile_images/1270234259/raffi-headshot-casual_normal.png", 
     "id": 8285392, 
     "listed_count": 619, 
     "profile_use_background_image": true, 
     "profile_text_color": "333333", 
     "followers_count": 18752, 
     "lang": "en", 
     "protected": false, 
     "geo_enabled": true, 
     "notifications": false, 
     "description": "Director of @twittereng's Platform Services. I break things.", 
     "profile_background_color": "C0DEED", 
     "verified": false, 
     "time_zone": "Pacific Time (US & Canada)", 
     "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme1/bg.png", 
     "statuses_count": 5007, 
     "profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png", 
     "default_profile_image": false, 
     "friends_count": 701, 
     "following": true, 
     "show_all_inline_media": true, 
     "screen_name": "raffi" 
    }, 
    "in_reply_to_screen_name": null, 
    "in_reply_to_status_id": null 
    }, 
    { 
    "coordinates": null, 
    "truncated": false, 
    "created_at": "Tue Aug 28 19:59:34 +0000 2012", 
    "favorited": false, 
    "id_str": "240539141056638977", 
    "in_reply_to_user_id_str": null, 
    "entities": { 
     "urls": [ 

     ], 
     "hashtags": [ 

     ], 
     "user_mentions": [ 

     ] 
    }, 
    "text": "You'd be right more often if you thought you were wrong.", 
    "contributors": null, 
    "id": 240539141056638977, 
    "retweet_count": 1, 
    "in_reply_to_status_id_str": null, 
    "geo": null, 
    "retweeted": false, 
    "in_reply_to_user_id": null, 
    "place": null, 
    "source": "web", 
    "user": { 
     "name": "Taylor Singletary", 
     "profile_sidebar_fill_color": "FBFBFB", 
     "profile_background_tile": true, 
     "profile_sidebar_border_color": "000000", 
     "profile_image_url": "http://a0.twimg.com/profile_images/2546730059/f6a8zq58mg1hn0ha8vie_normal.jpeg", 
     "created_at": "Wed Mar 07 22:23:19 +0000 2007", 
     "location": "San Francisco, CA", 
     "follow_request_sent": false, 
     "id_str": "819797", 
     "is_translator": false, 
     "profile_link_color": "c71818", 
     "entities": { 
     "url": { 
      "urls": [ 
      { 
       "expanded_url": "http://www.rebelmouse.com/episod/", 
       "url": "http://t.co/Lxw7upbN", 
       "indices": [ 
       0, 
       20 
       ], 
       "display_url": "rebelmouse.com/episod/" 
      } 
      ] 
     }, 
     "description": { 
      "urls": [ 

      ] 
     } 
     }, 
     "default_profile": false, 
     "url": "http://t.co/Lxw7upbN", 
     "contributors_enabled": false, 
     "favourites_count": 15990, 
     "utc_offset": -28800, 
     "profile_image_url_https": "https://si0.twimg.com/profile_images/2546730059/f6a8zq58mg1hn0ha8vie_normal.jpeg", 
     "id": 819797, 
     "listed_count": 340, 
     "profile_use_background_image": true, 
     "profile_text_color": "D20909", 
     "followers_count": 7126, 
     "lang": "en", 
     "protected": false, 
     "geo_enabled": true, 
     "notifications": false, 
     "description": "Reality Technician, Twitter API team, synthesizer enthusiast; a most excellent adventure in timelines. I know it's hard to believe in something you can't see.", 
     "profile_background_color": "000000", 
     "verified": false, 
     "time_zone": "Pacific Time (US & Canada)", 
     "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/643655842/hzfv12wini4q60zzrthg.png", 
     "statuses_count": 18076, 
     "profile_background_image_url": "http://a0.twimg.com/profile_background_images/643655842/hzfv12wini4q60zzrthg.png", 
     "default_profile_image": false, 
     "friends_count": 5444, 
     "following": true, 
     "show_all_inline_media": true, 
     "screen_name": "episod" 
    }, 
    "in_reply_to_screen_name": null, 
    "in_reply_to_status_id": null 
    } 
] 
+0

JArray을 사용할 수 있습니까? – Jason

+0

@ Jason 강하게 생성 된 클래스를 만들 것을 요구하는 경우 이미 완료했습니다. – Prad

+0

@ Jason하지만 JSON을 묻는다면 질문에 추가했습니다. – Prad

답변

0

귀하의 데이터는 JSON 배열, 그래서 대신 JObject 같이 twitterData 무엇

// data is your raw json string 
JArray json = JArray.Parse (data); 

// json[0] will be first element, json[1] will be second, etc