繁体   English   中英

解析JSON Feed iOS 5

[英]Parsing JSON Feed iOS 5

对于iOS开发,我还很陌生,我想解析JSON twitter feed的值,因此可以将它们引入tableView中。

到目前为止,我已经将提要中的信息以字符串的形式登录到控制台栏中,但是我仍在继续下一步。

我当前的代码是:

- (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"}]

然后,我想将其浮动到数组中,以便增加返回结果的数量,并循环遍历并将其显示在表中。

苹果最终在iOS 5中公开了其JSON解析器。

尝试看一下NSJSONSerialization

您的代码可能看起来像这样,但是我建议您先阅读文档。

    NSArray* yourArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:someError]
    - (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);
    }

这个给你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM