0

我有NSDictionary作为结果的对象NSJSONSerialization,它有一个名为列表的字典数组的子项。看看我的 json 结果:

{
  "tags": [
    "rofl",
    "lmao",
    "funny",
    "haha",
    "lmfao"
  ],
  "result_type": "exact",
  "list": [
    {
      "defid": 3689813,
      "word": "Lol",
      "author": "Lol B",
      "permalink": "http://lol.urbanup.com/3689813",
      "definition": "The name 'Lol' is an abreviated form of the name '[Laurence]'.",
      "example": "\"Hey Lol, you alright?\"\r\n\r\n\"..Well i was chattin to Lol and he said..\"",
      "thumbs_up": 44617,
      "thumbs_down": 9926,
      "current_vote": ""
    },
    ------- bla bla bla ----------

  ],
  "sounds": [
    "http://media.urbandictionary.com/sound/lol-871.mp3",
    ------- bla bla bla ----------
  ]
}

我还为项目(列表的子项)创建了一个类模型。

#import <Foundation/Foundation.h>

@interface Item : NSObject
@property (strong, nonatomic) NSString *defid;
@property (strong, nonatomic) NSString *word;
@property (strong, nonatomic) NSString *author;
@property (strong, nonatomic) NSURL *permalink;
@property (strong, nonatomic) NSString *definition;
@property (strong, nonatomic) NSString *example;

-(instancetype)initWithDictionary:(NSDictionary *)dict;
@end

我还创建了它的初始化程序

-(instancetype)initWithDictionary:(NSDictionary *)dict {
    if (self = [super init]) {
        self.defid = [dict valueForKey:@"defid"];
        self.word = [dict valueForKey:@"word"];
        self.author = [dict valueForKey:@"author"];
        self.permalink = [dict valueForKey:@"permalink"];
        self.definition = [dict valueForKey:@"definition"];
        self.example = [dict valueForKey:@"example"];
    }
    return self;
}

我已经创建了一个列表项数组 ( NSDictionary),我的问题是我想将它们映射到我的类中,以便创建列表项的实例。那么如何将数组中的项目转换为我的项目类? 我应该迭代数组的每个项目并调用该初始化吗?或任何优雅的方法来做到这一点?

附加问题

我对 iOS 开发比较陌生。我只想解析我的请求结果。是否有任何提示可以获取一些请求并将其结果分配给数据源一个表视图(数组)?这是我的获取方法:

- (void)fetchDataFromMashape:(NSURL *)URL {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    [request setHTTPMethod:@"GET"];
    [request setValue:API_KEY_MASHAPE forHTTPHeaderField:API_MASHAPE_HEADER_1];
    [request setValue:API_ACCEPT forHTTPHeaderField:API_MASHAPE_HEADER_2];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"Result: %@", jsonResult);

        _results = [[NSArray alloc] initWithArray:[jsonResult valueForKey:@"list"]];
    }];

    [task resume];
} 
4

1 回答 1

1

我的建议是创建一个以 json 列表为参数的静态方法

+ (NSArray<Item*>*) parseJson:(NSArray*) json {
       NSMutableArray<Item*>* results = [[NSMutableArray alloc] initWithCapacity:son.length];
       for (NSDictionary* anItem in json){
              [results append:[[Item alloc] initWithDictionary:anItem]];
       }

       return results;
}
于 2016-03-03T09:48:02.920 回答