0

我有NSDictionary对象作为NSJSONSerialization的结果,它的子对象是名为list的字典数组。看看我的JSON结果:作为提取HTTP请求的结果,从JSON映射NSDictionary

{ 
    "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]; 
} 
+0

我会建议你使用Github上的衬肩框架:https://开头github上。 COM /套/套。它消除了JSON的所有痛苦,以模拟序列化和反序列化。 – ZeMoon

+0

出于某种原因,我更喜欢现在使用苹果的框架,并尽量不要使用第三方库。但是,谢谢你的建议@ZeMoon –

+0

注意:在字典中获取值的指定方法是'objectForKey:'或者键引号('dict [@“key”]')。 'valueForKey:'是一种具有特殊行为的键值编码方法。只有在你真的需要它时才使用它。 – vadian

回答

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; 
} 
+0

啊,我明白了。谢谢! –