从JSON - Dictionary中获取对象

时间:2013-08-21 12:54:09

标签: objective-c json macos

我正在解析从twitter API获取的一些 JSON 。我设法找到了我需要的所有键,我怀疑是因为这个键可以有多个值。

以下是我用于其他所有内容的代码,以及我尝试获取的密钥的JSON结构,即媒体部分中的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"];
}

2 个答案:

答案 0 :(得分: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"];
        ...
    }
    ...
}

答案 1 :(得分:0)

实际上在意外地花了几个小时后对它进行了分类,这有效:

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

卫生署!

由于

加雷