Разбор JSON Feed iOS 5 - PullRequest
       4

Разбор JSON Feed iOS 5

2 голосов
/ 21 февраля 2012

Я довольно новичок в разработке для iOS, и я хочу проанализировать значения фида JSON в твиттере, чтобы я мог перетащить их в 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"}]

Затем я хочу добавить это в массив, чтобы я мог увеличить количество возвращаемых результатов, перебрать и отобразить их в таблице.

Ответы [ 2 ]

1 голос
/ 21 февраля 2012

В iOS 5 Apple наконец-то обнародовала свой JSON Parser.

Попробуйте посмотреть NSJSONSerialization

Ваш код может выглядеть следующим образом, но я предлагаю сначала прочитать Документы.

    NSArray* yourArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:someError]
0 голосов
/ 21 февраля 2012
    - (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);
    }

Вот вы:)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...