2012-02-21 82 views
2

我對iOS開發非常陌生,我想解析JSON twitter feed的值,所以我可以將它們拖入tableView。解析JSON Feed iOS 5

到目前爲止,我已將來自Feed的信息作爲字符串登錄到我的控制檯欄中,但我堅持要繼續下一步。

我當前的代碼是:

- (void)loadTweets 
{ 
NSString *twitterURL = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=%d", tweetCellCount];  
NSURL *fullURL = [NSURL URLWithString:twitterURL]; 

NSError *error = nil; 
NSData *dataURL = [NSData dataWithContentsOfURL:fullURL options:0 error:&error]; 

NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 

NSLog(@"Everything:\n%@", strResult); 
} 

這使我從進料的原始數據:

[{"id_str":"171930825225551872","coordinates":null,"in_reply_to_status_id_str":null,"place":null,"possibly_sensitive":false,"geo":null,"in_reply_to_user_id_str":null,"favorited":false,"created_at":"Tue Feb 21 12:14:37 +0000 2012","user":{"id":148344608,"profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png","created_at":"Wed May 26 13:24:44 +0000 2010","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1041551481\/twitter_logo_normal.jpg","profile_text_color":"333333","time_zone":null,"following":null,"followers_count":2441,"profile_sidebar_border_color":"C0DEED","screen_name":"evostikleague","verified":false,"utc_offset":null,"default_profile_image":false,"location":"UK","name":"The Evo-Stik League","notifications":null,"profile_background_tile":false,"favourites_count":0,"id_str":"148344608","profile_sidebar_fill_color":"DDEEF6","contributors_enabled":false,"protected":false,"show_all_inline_media":false,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1041551481\/twitter_logo_normal.jpg","profile_background_color":"C0DEED","description":"","default_profile":true,"lang":"en","is_translator":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png","statuses_count":1296,"geo_enabled":false,"profile_link_color":"0084B4","follow_request_sent":null,"friends_count":178,"listed_count":43,"profile_use_background_image":true,"url":"http:\/\/www.evostikleague.co.uk\/"},"in_reply_to_screen_name":null,"in_reply_to_user_id":null,"retweeted":false,"in_reply_to_status_id":null,"contributors":null,"truncated":false,"source":"\u003Ca href=\"http:\/\/twitterfeed.com\" rel=\"nofollow\"\u003Etwitterfeed\u003C\/a\u003E","id":171930825225551872,"retweet_count":1,"text":"The Evo-Stik Challenge: League announce sponsored bike ride from Wembley to Blackpool http:\/\/t.co\/23i8gC2N"},{"id_str":"171915587491078144","coordinates":null,"in_reply_to_status_id_str":null,"place":null,"possibly_sensitive":false,"geo":null,"in_reply_to_user_id_str":null,"favorited":false,"created_at":"Tue Feb 21 11:14:04 +0000 2012","user":{"id":148344608,"profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png","created_at":"Wed May 26 13:24:44 +0000 2010","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1041551481\/twitter_logo_normal.jpg","profile_text_color":"333333","time_zone":null,"following":null,"followers_count":2441,"profile_sidebar_border_color":"C0DEED","screen_name":"evostikleague","verified":false,"utc_offset":null,"default_profile_image":false,"location":"UK","name":"The Evo-Stik League","notifications":null,"profile_background_tile":false,"favourites_count":0,"id_str":"148344608","profile_sidebar_fill_color":"DDEEF6","contributors_enabled":false,"protected":false,"show_all_inline_media":false,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1041551481\/twitter_logo_normal.jpg","profile_background_color":"C0DEED","description":"","default_profile":true,"lang":"en","is_translator":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png","statuses_count":1296,"geo_enabled":false,"profile_link_color":"0084B4","follow_request_sent":null,"friends_count":178,"listed_count":43,"profile_use_background_image":true,"url":"http:\/\/www.evostikleague.co.uk\/"},"in_reply_to_screen_name":null,"in_reply_to_user_id":null,"retweeted":false,"in_reply_to_status_id":null,"contributors":null,"truncated":false,"source":"\u003Ca href=\"http:\/\/twitterfeed.com\" rel=\"nofollow\"\u003Etwitterfeed\u003C\/a\u003E","id":171915587491078144,"retweet_count":0,"text":"Gingerbreads look to consolidate top spot: Grantham Town update, by Graham Cowell http:\/\/t.co\/FREdshtm"},{"id_str":"171633185870979074","coordinates":null,"in_reply_to_status_id_str":null,"place":null,"possibly_sensitive":false,"geo":null,"in_reply_to_user_id_str":null,"favorited":false,"created_at":"Mon Feb 20 16:31:54 +0000 2012","user":{"id":148344608,"profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png","created_at":"Wed May 26 13:24:44 +0000 2010","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1041551481\/twitter_logo_normal.jpg","profile_text_color":"333333","time_zone":null,"following":null,"followers_count":2441,"profile_sidebar_border_color":"C0DEED","screen_name":"evostikleague","verified":false,"utc_offset":null,"default_profile_image":false,"location":"UK","name":"The Evo-Stik League","notifications":null,"profile_background_tile":false,"favourites_count":0,"id_str":"148344608","profile_sidebar_fill_color":"DDEEF6","contributors_enabled":false,"protected":false,"show_all_inline_media":false,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1041551481\/twitter_logo_normal.jpg","profile_background_color":"C0DEED","description":"","default_profile":true,"lang":"en","is_translator":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png","statuses_count":1296,"geo_enabled":false,"profile_link_color":"0084B4","follow_request_sent":null,"friends_count":178,"listed_count":43,"profile_use_background_image":true,"url":"http:\/\/www.evostikleague.co.uk\/"},"in_reply_to_screen_name":null,"in_reply_to_user_id":null,"retweeted":false,"in_reply_to_status_id":null,"contributors":null,"truncated":false,"source":"\u003Ca href=\"http:\/\/twitterfeed.com\" rel=\"nofollow\"\u003Etwitterfeed\u003C\/a\u003E","id":171633185870979074,"retweet_count":1,"text":"Chasetown unveil \u00a310k Makeover: Premier Division club won Evo-Stik Facilities Makeover competition http:\/\/t.co\/ywf74N9f"}] 

我當時想鮑勃到一個數組這一點,這樣我就可以加量返回結果,並循環並顯示在表中。

回答

1

在iOS 5中,Apple最終公開了他們的JSON解析器。

試着看一下NSJSONSerialization

您的代碼看起來是這樣,但我建議首先閱讀文檔。

NSArray* yourArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:someError] 
+2

您可能需要使用JSONKit代替,因爲NSJSONSerialization僅在iOS 5及更高版本。雖然:)相當方便:) – 2012-02-22 02:03:05

0
- (void)loadTweets 
    { 
    NSString *twitterURL = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=%d", tweetCellCount];  
    NSURL *fullURL = [NSURL URLWithString:twitterURL]; 

    NSError *error = nil; 
    NSData *dataURL = [NSData dataWithContentsOfURL:fullURL options:0 error:&error]; 
    id result=[NSJSONSerialization JSONObjectWithData:dataURL options: 
        NSJSONReadingMutableContainers error:&error]; 
    NSLog("JSON data:%@", result); 
    //NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; 

    //NSLog(@"Everything:\n%@", strResult); 
    } 

給你:)