2011-11-29 181 views
4

我以JSON格式獲得響應,並且從未解析過它,所以有人可以幫我理解這一點。 這裏的JSON響應我得到:如何從JSON中獲取數據?

微博迴應:

(
    { 
     contributors = "<null>"; 
     coordinates = "<null>"; 
     "created_at" = "Tue Nov 29 15:48:35 +0000 2011"; 
     entities =   { 
      hashtags =    (
      ); 
      media =    (
           { 
        "display_url" = "pic.twitter.com/650E1WRY"; 
        "expanded_url" = "http://twitter.com/ashu1702/status/141544088850796545/photo/1"; 
        id = 141544088854990848; 
        "id_str" = 141544088854990848; 
        indices =      (
         22, 
         42 
        ); 
        "media_url" = "http://p.twimg.com/AfbdmVBCEAAPJvT.jpg"; 
        "media_url_https" = "https://p.twimg.com/AfbdmVBCEAAPJvT.jpg"; 
        sizes =      { 
         large =       { 
          h = 279; 
          resize = fit; 
          w = 215; 
         }; 
         medium =       { 
          h = 279; 
          resize = fit; 
          w = 215; 
         }; 
         small =       { 
          h = 279; 
          resize = fit; 
          w = 215; 
         }; 
         thumb =       { 
          h = 150; 
          resize = crop; 
          w = 150; 
         }; 
        }; 
        type = photo; 
        url = "http://t.co/650E1WRY"; 
       } 
      ); 
      urls =    (
      ); 
      "user_mentions" =    (
      ); 
     }; 
     favorited = 0; 
     geo = "<null>"; 
     id = 141544088850796545; 
     "id_str" = 141544088850796545; 
     "in_reply_to_screen_name" = "<null>"; 
     "in_reply_to_status_id" = "<null>"; 
     "in_reply_to_status_id_str" = "<null>"; 
     "in_reply_to_user_id" = "<null>"; 
     "in_reply_to_user_id_str" = "<null>"; 
     place = "<null>"; 
     "possibly_sensitive" = 0; 
     "retweet_count" = 0; 
     retweeted = 0; 
     source = "<a href=\"http://www.apple.com\" rel=\"nofollow\">iOS</a>"; 
     text = "I am in Syria @(null) http://t.co/650E1WRY"; 
     truncated = 0; 
     user =   { 
      "contributors_enabled" = 0; 
      "created_at" = "Sun May 01 15:20:52 +0000 2011"; 
      "default_profile" = 1; 
      "default_profile_image" = 1; 
      description = "<null>"; 
      "favourites_count" = 0; 
      "follow_request_sent" = "<null>"; 
      "followers_count" = 0; 
      following = "<null>"; 
      "friends_count" = 5; 
      "geo_enabled" = 0; 
      id = 291164338; 
      "id_str" = 291164338; 
      "is_translator" = 0; 
      lang = en; 
      "listed_count" = 0; 
      location = "<null>"; 
      name = "Ashutosh Tiwari"; 
      notifications = "<null>"; 
      "profile_background_color" = C0DEED; 
      "profile_background_image_url" = "http://a0.twimg.com/images/themes/theme1/bg.png"; 
      "profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme1/bg.png"; 
      "profile_background_tile" = 0; 
      "profile_image_url" = "http://a2.twimg.com/sticky/default_profile_images/default_profile_3_normal.png"; 
      "profile_image_url_https" = "https://si0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png"; 
      "profile_link_color" = 0084B4; 
      "profile_sidebar_border_color" = C0DEED; 
      "profile_sidebar_fill_color" = DDEEF6; 
      "profile_text_color" = 333333; 
      "profile_use_background_image" = 1; 
      protected = 0; 
      "screen_name" = ashu1702; 
      "show_all_inline_media" = 0; 
      "statuses_count" = 62; 
      "time_zone" = "<null>"; 
      url = "<null>"; 
      "utc_offset" = "<null>"; 
      verified = 0; 
     }; 
    } 
) 

具體來說,我需要的只是「expanded_url」,我在我的代碼,這樣做是爲了得到它:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];// where response data is coming from the call made to twitter timeline_api 

          NSString * callBackURL = [dict objectForKey:@"expanded_url"]; 

在做這個我得到這個錯誤:

2011-11-29 09:50:31.214 L'Occitane[1119:19d07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x85a50d0 
2011-11-29 09:50:31.215 L'Occitane[1119:19d07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x85a50d0' 

PLease help !!!

謝謝,

回答

7

當轉換爲可可Foundation對象,JSON分解成一束嵌套NSArraysNSDictionary S的最終導致NSString S,NSNumber S和NSNull秒。理想情況下,您可以從Web服務的文檔轉發,但您可以從根對象通過NSLog打印的描述向後工作。

從您的日誌中,我可以看到expanded_url是未命名對象的成員,它是對象entities對象的成員中的第一個條目。包含entities的未命名對象是數組的一部分,這是JSON解串器將返回的內容。所以,你想這樣做(如果長格式博覽會寫出來,沒有任何驗證):

NSArray *result = [NSJSONSerialization ... 

NSDictionary *firstObject = [result objectAtIndex:0]; 
NSDictionary *entities = [firstObject objectForKey:@"entities"]; 
NSArray *media = [entities objectForKey:@"media"]; 
NSDictionary *relevantObject = [media objectAtIndex:0]; 
NSString *expandedUrl = [relevantObject objectForKey:@"expanded_url"]; 

的主要風險在那裏的是,Web服務可能會返回一個空數組爲media,造成objectAtIndex:0引發一個異常,或者它會返回除數組之外的其他東西作爲根對象,或者以某種方式調整某行中某處的類型。 Objective-C關於消息傳遞的通常規則nil應該可以幫助您避免使用字典時遇到的大多數問題。相當多的驗證適用於判斷電話。

+0

非常感謝! – Ashutosh