2016-02-29 79 views
-2

如果這是一個新手問題,我們抱歉,但我一直在努力解決這個問題,但沒有運氣。我正在嘗試使用AFOAuth2Manager框架來拉取用戶的Twitter時間表。除了返回的JSON對象是一個數組並且整個對象是一個巨型對象之外,一切都有意義。我顯然希望將其分解成不同的元素並將它們存儲在字典中,但迄今尚未能弄清楚。將單個數組對象分解爲許多對象

這是數組對象外觀的一個非常部分的例子。完整的對象是大約20個附加了這種格式的推文。如果需要,我會發布整個json對象,但發佈整個事情似乎毫無意義。

繼承人我的代碼:

[manager GET:@"/1.1/statuses/user_timeline.json?screen_name=jack" 
     parameters:nil 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 

      self.object = responseObject; 
      NSLog(@"Success: %@", responseObject); 

      if ([responseObject isKindOfClass:[NSArray class]]) { 
       NSLog(@"object is a nsarray class"); 
      } else if ([responseObject isKindOfClass:[NSDictionary class]]){ 
       NSLog(@"object is a nsdictionary class"); 
      } else { 
       NSLog(@"object is a different class"); 
      } 

      NSArray *response = [NSArray arrayWithObject:responseObject]; 

      NSLog(@"count %ld", [response count]); 

      NSData *dataFromTwitter = [NSKeyedArchiver archivedDataWithRootObject:self.object]; 

      NSError *parseError = nil; 
      NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:dataFromTwitter 
                      options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers|NSJSONReadingAllowFragments 
                      error:&parseError]; 

      NSLog(@"response Dict: %@", responseDict); 




      NSError *e = nil; 
      NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:dataFromTwitter 
                    options:NSJSONReadingAllowFragments 
                    error:&e]; 

      NSLog(@"jsonArray: %@", jsonArray); 

      NSError *jsonError2 = nil; 

      id jsonObject = [NSJSONSerialization JSONObjectWithData:dataFromTwitter 
                  options:NSJSONReadingAllowFragments 
                   error:&jsonError2]; 



      if ([jsonObject isKindOfClass:[NSArray class]]) { 
       NSLog(@"its an array!"); 
       NSArray *jsonArray = (NSArray *)jsonObject; 
       NSLog(@"jsonArray2 - %@",jsonArray); 
      } 
      else { 
       NSLog(@"its probably a dictionary"); 
       NSDictionary *jsonDictionary = (NSDictionary *)jsonObject; 
       NSLog(@"jsonDictionary - %@",jsonDictionary); 
       NSLog(@"error %@", jsonError2); 
      } 



     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Failure: %@", error); 
     }]; 





{ 
    contributors = "<null>"; 
    coordinates = "<null>"; 
    "created_at" = "Mon Feb 29 01:18:05 +0000 2016"; 
    entities =   { 
     hashtags =    (
     ); 
     symbols =    (
     ); 
     urls =    (
     ); 
     "user_mentions" =    (
          { 
       id = 14616957; 
       "id_str" = 14616957; 
       indices =      (
        0, 
        7 
       ); 
       name = "Jason Del Rey"; 
       "screen_name" = DelRey; 
      }, 
          { 
       id = 19040598; 
       "id_str" = 19040598; 
       indices =      (
        8, 
        18 
       ); 
       name = "\U0ca0_\U0ca0"; 
       "screen_name" = MikeIsaac; 
      }, 
          { 
       id = 46063; 
       "id_str" = 46063; 
       indices =      (
        19, 
        30 
       ); 
       name = "Hunter Walk"; 
       "screen_name" = hunterwalk; 
      } 
     ); 
    }; 
    "favorite_count" = 12; 
    favorited = 0; 
    geo = "<null>"; 
    id = 704113377920978944; 
    "id_str" = 704113377920978944; 
    "in_reply_to_screen_name" = DelRey; 
    "in_reply_to_status_id" = 704112911116013568; 
    "in_reply_to_status_id_str" = 704112911116013568; 
    "in_reply_to_user_id" = 14616957; 
    "in_reply_to_user_id_str" = 14616957; 
    "is_quote_status" = 0; 
    lang = en; 
    place = "<null>"; 
    "retweet_count" = 0; 
    retweeted = 0; 
    source = "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>"; 
    text = "@DelRey @MikeIsaac @hunterwalk this changes everything"; 
    truncated = 0; 
    user =   { 
     "contributors_enabled" = 0; 
     "created_at" = "Tue Mar 21 20:50:14 +0000 2006"; 
     "default_profile" = 0; 
     "default_profile_image" = 0; 
     description = "#withMalala!"; 
     entities =    { 
      description =     { 
       urls =      (
       ); 
      }; 
     }; 
     "favourites_count" = 11433; 
     "follow_request_sent" = "<null>"; 
     "followers_count" = 3458722; 
     following = "<null>"; 
     "friends_count" = 1859; 
     "geo_enabled" = 1; 
     "has_extended_profile" = 1; 
     id = 12; 
     "id_str" = 12; 
     "is_translation_enabled" = 0; 
     "is_translator" = 0; 
     lang = en; 
     "listed_count" = 25944; 
     location = "California, USA"; 
     name = Jack; 
     notifications = "<null>"; 
     "profile_background_color" = EBEBEB; 
     "profile_background_image_url" = "http://abs.twimg.com/images/themes/theme7/bg.gif"; 
     "profile_background_image_url_https" = "https://abs.twimg.com/images/themes/theme7/bg.gif"; 
     "profile_background_tile" = 0; 
     "profile_image_url" = "http://pbs.twimg.com/profile_images/668328458519384064/FSAIjKRl_normal.jpg"; 
     "profile_image_url_https" = "https://pbs.twimg.com/profile_images/668328458519384064/FSAIjKRl_normal.jpg"; 
     "profile_link_color" = 990000; 
     "profile_sidebar_border_color" = DFDFDF; 
     "profile_sidebar_fill_color" = F3F3F3; 
     "profile_text_color" = 333333; 
     "profile_use_background_image" = 1; 
     protected = 0; 
     "screen_name" = jack; 
     "statuses_count" = 19066; 
     "time_zone" = "Pacific Time (US & Canada)"; 
     url = "<null>"; 
     "utc_offset" = "-28800"; 
     verified = 1; 
    }; 
}, 
+0

這不是一個數組;這是一本字典。你究竟想用它做什麼? –

+0

多數民衆贊成在我以爲最初,但是當我運行以下,它說,這是一個數組 –

+0

if([responseObject isKindOfClass:[NSArray class]]){ NSLog(@「object is a nsarray class」); (@ responseObject isKindOfClass:[NSDictionary class]]){ NSLog(@「object is a nsdictionary class」);其他{ NSLog(@「object is a different class」); } –

回答

0

你顯示你的問題沒有代碼,在評論你的國家,你有一個responseObject,但你並不怎麼走過來的。

你是從什麼地方得到將是文本的JSON,你可以把它作爲一個NSStringNSData,一個NSArray其元素是這些,等

您需要獲取文本,然後解析它作爲JSON ,該NSJSONSerialization類將處理解析你,它的描述狀態的第一行:

您使用NSJSONSerialization類JSON轉換爲基礎對象和轉換Foundation對象到JSON。

一旦你有這些基礎的對象 - 數組,字典,字符串,數字等,你可以提取領域你有興趣

一旦你如果它不寫一些代碼來做到這一點。沒有工作,你卡住了問一個新的問題,顯示你的代碼,有人可能會進一步幫助你。

HTH

+0

感謝您的反饋。我在我的問題中添加了代碼。 –

相關問題