【问题标题】:Cannot access NSDictionary key in FourSquare API venues无法访问 FourSquare API 场所中的 NSDictionary 键
【发布时间】:2015-01-16 12:49:42
【问题描述】:

我正在尝试访问以下 FourSquare NSDictionary 中的“前缀”和“后缀”字符串。在我的 nsobject 中,我尝试了以下操作,但没有成功。有没有简单的方法来访问字符串?我试过了:

venue.photo = v[@"photo"];
venue.photo = v[@"photo"][@"prefix"];

和 NSDictionary 打印:

{
    categories =     (
                {
            icon =             {
                prefix = "https://ss3.4sqi.net/img/categories_v2/arts_entertainment/movietheater_";
                suffix = ".png";
            };
            id = 4bf58dd8d48988d17f941735;
            name = "Movie Theater";
            pluralName = "Movie Theaters";
            primary = 1;
            shortName = "Movie Theater";
        }
    );
    contact =     {
        formattedPhone = "(844) 462-7342";
        phone = 8444627342;
        twitter = regalmovies;
    };
    events =     {
        count = 9;
        summary = "9 movies";
    };
    hasMenu = 1;
    hereNow =     {
        count = 2;
        groups =         (
                        {
                count = 2;
                items =                 (
                );
                name = "Other people here";
                type = others;
            }
        );
        summary = "2 people are checked in here";
    };
    id = 4a2aac37f964a52034961fe3;
    location =     {
        address = "1471 W Webster Ave";
        cc = US;
        city = Chicago;
        country = "United States";
        formattedAddress =         (
            "1471 W Webster Ave",
            "Chicago, IL 60614",
            "United States"
        );
        lat = "41.92149057";
        lng = "-87.66487751";
        postalCode = 60614;
        state = IL;
    };
    menu =     {
        anchor = "View Menu";
        label = Menu;
        mobileUrl = "https://foursquare.com/v/4a2aac37f964a52034961fe3/device_menu";
        type = Menu;
        url = "https://foursquare.com/v/regal-webster-place-11/4a2aac37f964a52034961fe3/menu";
    };
    name = "Regal Webster Place 11";
    referralId = "v-1416373439";
    specials =     {
        count = 1;
        items =         (
                        {
                description = "";
                icon = default;
                id = 50355f96d86cbef3478adc65;
                interaction =                 {
                    entryUrl = "https://foursquare.com/device/specials/50355f96d86cbef3478adc65?venueId=4a2aac37f964a52034961fe3";
                };
                message = "Join us for Deal Days! ALL DAY Tuesday enjoy $6.50 tickets.";
                page =                 {
                    bio = "";
                    contact =                     {
                        twitter = regalmovies;
                    };
                    firstName = "Regal Cinemas";
                    followers =                     {
                        count = 12203;
                        groups =                         (
                        );
                    };
                    gender = none;
                    homeCity = "Knoxville, TN";
                    id = 12864010;
                    photo =                     {
                        prefix = "https://irs0.4sqi.net/img/user/";
                        suffix = "/DMGZGZVXT3NVLKNP.jpg";
                    };
                    tips =                     {
                        count = 347;
                    };
                    type = chain;
                };
                provider = foursquare;
                redemption = webview;
                state = unlocked;
                title = Special;
                type = frequency;
                unlocked = 1;
            }
        );
    };
    stats =     {
        checkinsCount = 23578;
        tipCount = 56;
        usersCount = 10220;
    };
    storeId = Chicago;
    verified = 1;
}

【问题讨论】:

  • 首先,您打印的是字典,而不是数组。似乎这本字典具有“项目”的价值,它是一个字典数组(在这种情况下只有一个元素)。 items 数组中的第一个字典具有键“page”的字典。页面字典包含键“照片”的字典,其中包含键“前缀”和“后缀”的字符串
  • 感谢您指出这一点(我还在学习)。我已经更新了问题并使用了:venue.photo = v[@"specials"][@"items"][@"page"][@"photo"][@"prefix"];但没有运气。

标签: ios json dictionary nsarray foursquare


【解决方案1】:

以下代码应该可以工作 -

NSArray *itemsArray=(NSArray *)venue[@"items"];
for (NSDictionary *specialDictionary in itemsArray) {
    NSDictionary *pageDictionary=(NSDictionary *)specialDictionary[@"page"];
    NSDictionary *photoDictionary=(NSDictionary *)pageDictionary[@"photo"];
    NSString *prefix=(NSString *)photoDictionary[@"prefix"];
    NSString *suffix=(NSString *)photoDictionary[@"suffix"];
    // Do something with prefix and suffix 
}

【讨论】:

  • 谢谢,但我收到错误“预期的读取字典元素的方法在“FSVenue”类型的对象上找不到。经过研究,我是否在我的“FSVenue”对象中添加 objectForKeyedSubscript: 来解析?
  • FSVenue 是哪个对象?似乎您没有普通的 NSDictionary,但是您正在使用某个库?您正在使用的库的 URL 是什么。
  • 这是 FourSquare 场地/搜索 API - api.foursquare.com/v2/venues/search。你指的是这个吗?
  • 不,从错误消息来看,venue 是什么类型的对象,它似乎是一个 FSVenue,而不是你可以从 JSONParser 返回的普通 NSDictionary。您是否使用某些框架或库与 API 进行交互?
  • 你说得对,地点是 FSVenue,很抱歉造成混乱。我正在使用康斯坦丁的 github github.com/Constantine-Fry/Foursquare-API-v2 调用使用 NSURLRequest,如果这回答了您的 URL 问题。
【解决方案2】:

注意关键项中有一个小括号“(”,这一行

items =         ( <----
                        {
                description = "";
                icon = default;
                id = 50355f96d86cbef3478adc65;
                interaction =                 {

特色,项目[0],页面,照片,前缀 所以正确答案应该是

 venue.photo = v[@"specials"][@"items"][0][@"page"][@"photo"][@"prefix"];

【讨论】:

  • 我试了一下,但出现错误:'NSRangeException',原因:'*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
  • 可以先查看物品的大小 NSLog(@"size : %d",v[@"specials"][@"items"].size);猜测 items 数组有时可能是空的??
猜你喜欢
  • 2022-08-05
  • 2016-03-11
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-25
  • 2017-04-27
相关资源
最近更新 更多