2013-08-21 75 views
0

我解析了一些JSON從twitter API中獲取。我設法找到所需的所有密鑰,除此之外,我懷疑這是因爲可能有多個此密鑰值。從JSON獲取對象 - 詞典

這裏是我用於其他所有代碼的代碼,以及我試圖獲取的密鑰JSON的結構,它是media部分中的media_url。

任何人都有關於如何訪問此值的指針。我試過media.media_url,但那不起作用,應該是我應該預料的,假設它似乎是它自己的字典。

幫助非常感謝!

感謝

加雷思

編輯1

我也曾嘗試做以下,但我得到空的對象

 NSArray *media = dict[@"media"]; 
    NSDictionary *media0 = media[0]; 
    NSString *display_url = media0[@"display_url"];  

但做骰子:

(
{ 
    contributors = "<null>"; 
    coordinates = "<null>"; 
    "created_at" = "Tue Aug 20 08:33:59 +0000 2013"; 
    entities =   { 
     hashtags =    (
          { 
       indices =      (
        0, 
        9 
       ); 
       text = verygood; 
      } 
     ); 
     media =    (
          { 
       "display_url" = "pic.twitter.com/4Pw6QaotCf"; 
       "expanded_url" = "http://twitter.com/gazzer82/status/369739076271742977/photo/1"; 
       id = 369739076032667648; 
       "id_str" = 369739076032667648; 
       indices =      (
        10, 
        32 
       ); 
       "media_url" = "http://pbs.twimg.com/media/BSGTuw6CIAAf3Us.jpg"; 
       "media_url_https" = "https://pbs.twimg.com/media/BSGTuw6CIAAf3Us.jpg"; 
       sizes =      { 
        large =       { 
         h = 768; 
         resize = fit; 
         w = 1024; 
        }; 
        medium =       { 
         h = 450; 
         resize = fit; 
         w = 600; 
        }; 
        small =       { 
         h = 255; 
         resize = fit; 
         w = 340; 
        }; 
        thumb =       { 
         h = 150; 
         resize = crop; 
         w = 150; 
        }; 
       }; 
       type = photo; 
       url = "http://t.co/4Pw6QaotCf"; 
      } 
     ); 
     symbols =    (
     ); 
     urls =    (
     ); 
     "user_mentions" =    (
     ); 
    }; 
    "favorite_count" = 0; 
    favorited = 0; 
    geo = "<null>"; 
    id = 369739076271742977; 
    "id_str" = 369739076271742977; 
    "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>"; 
    lang = en; 
    place = "<null>"; 
    "possibly_sensitive" = 0; 
    "retweet_count" = 0; 
    retweeted = 0; 
    source = "<a href=\"http://www.apple.com/\" rel=\"nofollow\">OS X</a>"; 
    text = "#verygood http://t.co/4Pw6QaotCf"; 
    truncated = 0; 
    user =   { 
     "contributors_enabled" = 0; 
     "created_at" = "Mon Nov 17 12:20:17 +0000 2008"; 
     "default_profile" = 0; 
     "default_profile_image" = 0; 
     description = "Video and Projection project manager for @sss_uk living in Cardiff. Hater of Cheese, lover of Red Wine."; 
     entities =    { 
      description =     { 
       urls =      (
       ); 
      }; 
     }; 
     "favourites_count" = 12; 
     "follow_request_sent" = 0; 
     "followers_count" = 67; 
     following = 0; 
     "friends_count" = 225; 
     "geo_enabled" = 1; 
     id = 17440336; 
     "id_str" = 17440336; 
     "is_translator" = 0; 
     lang = en; 
     "listed_count" = 0; 
     location = "51.477028,-3.18741"; 
     name = gazzer82; 
     notifications = 0; 
     "profile_background_color" = 352726; 
     "profile_background_image_url" = "http://a0.twimg.com/images/themes/theme5/bg.gif"; 
     "profile_background_image_url_https" = "https://si0.twimg.com/images/themes/theme5/bg.gif"; 
     "profile_background_tile" = 0; 
     "profile_image_url" = "http://a0.twimg.com/profile_images/1900659547/gareth-hussain_normal.jpg"; 
     "profile_image_url_https" = "https://si0.twimg.com/profile_images/1900659547/gareth-hussain_normal.jpg"; 
     "profile_link_color" = D02B55; 
     "profile_sidebar_border_color" = 829D5E; 
     "profile_sidebar_fill_color" = 99CC33; 
     "profile_text_color" = 3E4415; 
     "profile_use_background_image" = 1; 
     protected = 0; 
     "screen_name" = gazzer82; 
     "statuses_count" = 897; 
     "time_zone" = London; 
     url = "<null>"; 
     "utc_offset" = 3600; 
     verified = 0; 
    }; 
} 
) 


    for (NSDictionary *dict in filteredTweets) { 

    //Set the variables 
    NSString *userTweet = [dict valueForKey:@"text"]; 
    NSString *userScreenName = [dict valueForKeyPath:@"user.screen_name"]; 
    NSString *userRealName = [dict valueForKeyPath:@"user.name"]; 
    NSString *tweetDate = [dict valueForKey:@"created_at"]; 
    NSString *avatarURL = [dict valueForKeyPath:@"user.profile_image_url"]; 
    NSString *tweetIDString = [dict valueForKeyPath:@"id_str"]; 
    NSString *imagePath = [dict valueForKeyPath:@"media..media_url"]; 
} 
+0

的'media'字典似乎是裏面的'entities'字典? – trojanfoe

回答

0

其實排序它偶然在花費小時後,這個工程:

NSDictionary *entity = dict[@"entities"]; 
    NSArray *media  = entity[@"media"]; 
    NSDictionary *media0 = media[0]; 
    NSString *media_url = media0[@"media_url"]; 

衛生署!

感謝

加雷思

0

字典只是一個字典,無論您是否從JSON獲取它,以及它是否嵌套。

for (NSDictionary *dict in filteredTweets) { 
    NSDictionary *entities = dict[@"entities"]; 
    NSArray *media = entities[@"media"]; 
    for(NSDictionary *medium in media) { 
     NSString *media_url = medium[@"media_url"]; 
     ... 
    } 
    ... 
} 
+0

是的,有一個ureka /我幾乎只要我發佈它就成爲一個白癡時刻,有時你看不到樹木! –