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

啊,我明白了。謝謝! –