使用jsonmodel解析字典字典

时间:2017-01-22 12:06:46

标签: objective-c json dictionary jsonmodel

我正在使用JSONModel来处理JSON Api,现在却无法解析字典词典。

{
  "locations": {
    "one": {
      "displayName": "Name One",
      "description": "Description One"
    },
    "two": {
      "displayName": "Name Two",
      "description": "Description Two"
    }
  }
}

在我的例子中,我需要键“one”和“two”及其内容,所以我想到这样的事情

@protocol BaseDataModel;

@interface BaseDataModel : JSONModel

@property (nonatomic) NSDictionary<NSString *, LocationModel> *locations;

@end

但这不起作用,因为LocationModel不是Objective-C类型。

2 个答案:

答案 0 :(得分:1)

您应该只创建LocationModel类:

@interface LocationModel : JSONModel

@property (nonatomic) NSString * displayName;
@property (nonatomic) NSString * description;

@end

答案 1 :(得分:0)

您只是错过了LocationModel我认为

的@protocol声明
#import "JSONModel.h"

@protocol LocationModel;
@interface LocationModel : JSONModel

@property (nonatomic, retain) NSString *displayName;
@property (nonatomic, retain) NSString *description;

@end