用Restkit映射Forsquare中的嵌套数组

4

我正在尝试学习如何使用restKit从foursquare获取一些数据。

我使用了这个教程

使用了这个代码

我试图做出一些不同的东西,所以我尝试从类别中获取数据。

以下是foursqure的json长什么样子

   {
  venue: {
    id: "40a55d80f964a52020f31ee3",
    name: "Clinton St. Baking Co. & Restaurant",
    contact: {
      phone: "6466026263",
      formattedPhone: "(646) 602-6263"
    },
    location: {
      address: "4 Clinton St",
      crossStreet: "at E Houston St.",
      lat: 40.721294,
      lng: -73.983994,
      postalCode: "10002",
      city: "New York",
      state: "NY",
      country: "United States",
      cc: "US"
    },
    canonicalUrl: "https://foursquare.com/v/clinton-st-baking-co--restaurant/40a55d80f964a52020f31ee3",
    categories: [
      {
        id: "4bf58dd8d48988d143941735",
        name: "Breakfast Spot",
        pluralName: "Breakfast Spots",
        shortName: "Breakfast / Brunch",
        icon: {
          prefix: "https://foursquare.com/img/categories_v2/food/breakfast_",
          suffix: ".png"
        },
        primary: true
      },
      {
        id: "4bf58dd8d48988d16a941735",
        name: "Bakery",
        pluralName: "Bakeries",
        shortName: "Bakery",
        icon: {
          prefix: "https://foursquare.com/img/categories_v2/food/bakery_",
          suffix: ".png"
        }
      },
      {
        id: "4bf58dd8d48988d16d941735",
        name: "Café",
        pluralName: "Cafés",
        shortName: "Café",
        icon: {
          prefix: "https://foursquare.com/img/categories_v2/food/cafe_",
          suffix: ".png"
        }
      }
    ],

  }
}

以下是有关获取统计信息的教程部分

@interface Venue : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) Stats *stats;


@interface Stats : NSObject

@property (nonatomic, strong) NSNumber *checkinsCount;
@property (nonatomic, strong) NSNumber *tipCount;
@property (nonatomic, strong) NSNumber *usersCount;

映射部分
RKObjectMapping *statsMapping = [RKObjectMapping mappingForClass:[Stats class]];
    [statsMapping addAttributeMappingsFromArray:@[@"checkinsCount", @"tipCount", @"usersCount"]];
    RKRelationshipMapping *statsRelation = [RKRelationshipMapping relationshipMappingFromKeyPath:@"stats" toKeyPath:@"stats" withMapping:statsMapping];
    [venueMapping addPropertyMapping:statsRelation];

映射后。

我可以轻松获得以下数据

NSLog(@"%i",venue.stats.checkinsCount);

然后它会打印出checkinsCount数字。

现在我尝试获取类别数据。我修复了代码以获取新的数据。

添加以下内容:

@interface Venue : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) Categories *categories;

@interface Categories : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic ,strong) NSString *pluralName;

添加类似以下的映射:
RKObjectMapping *categoriesMapping = [RKObjectMapping mappingForClass:[Categories class]];
    [categoriesMapping addAttributeMappingsFromDictionary:@{@"name":@"name",@"pluralName":@"pluralName"}];
    RKRelationshipMapping *cate = [RKRelationshipMapping relationshipMappingFromKeyPath:@"categories" toKeyPath:@"categories" withMapping:categoriesMapping];
    [venueMapping addPropertyMapping:cate];

现在我试图获取类别名称

NSLog(@"%@",venue.categories.pluralName);

但它总是返回 null。

我认为问题可能在于数据类型?

有三个名称,我不知道如何将它们全部打印出来。

抱歉,我的母语不是英语,所以我无法很好地解释情况。


当你说它不起作用时,你是指什么?你期望发生什么与实际发生的有何不同? - Steve
等一下,我忘记打那个了,谢谢。 - Exiling
1个回答

8
在JSON中,categories是一个数组,所以你要映射它的属性应该是:
@property (strong, nonatomic) NSArray *categories;

您的登录代码需要更改为迭代类别:

for (Categories *category in venue.categories) {
    NSLog(@"%@", category.pluralName);
}

类别(Categories)这个名称有些误导,因为它实际上不是一个集合对象(似乎您最初的意图是这样)。

我尝试了一下,但是我打印出来的数据看起来像<Categories: 0x18d29e40>,<Categories: 0x18daf3d0>,我该如何获取类别名称?谢谢。 - Exiling
明白了,我想我忘记了它是一个数组类型。犯了个愚蠢的错误。非常感谢! - Exiling
分类迭代点赞 - Jakub Truhlář

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接