0

我正在解析一些从 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"];
}
4

2 回答 2

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"];
        ...
    }
    ...
}
于 2013-08-21T13:03:30.853 回答
0

实际上在意外花费数小时后对其进行了排序,这有效:

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

嗬!

谢谢

加雷斯

于 2013-08-21T13:07:14.350 回答