這些都是JSON解析字典或其他原語的良好框架,但如果您希望避免做大量重複性工作,請查看http://restkit.org。具體而言,請查看https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md這是對象映射的示例,您可以爲Teacher類定義映射,並使用KVC將json自動轉換爲Teacher對象。如果你使用RestKit的網絡調用,這個過程是透明和簡單的,但我已經有了我的網絡調用,我需要的是將我的JSON響應文本轉換爲一個用戶對象(在你的情況下,教師),我終於想通了怎麼樣。如果這是您需要的,請發表評論,我將分享如何使用RestKit進行操作。
注意:我將假定json是使用映射約定{"teacher": { "id" : 45, "name" : "Teacher McTeacher"}}
輸出的。如果不是這樣,而是像這樣{"id" : 45, "name" : "Teacher McTeacher"}
,那麼不要擔心...鏈接中的對象映射設計文檔會告訴您如何執行此操作......只需幾個額外的步驟,但不是太糟糕。
這是ASIHTTPRequest
- (void)requestFinished:(ASIHTTPRequest *)request {
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:[request.responseHeaders valueForKey:@"Content-Type"]]; // i'm assuming your response Content-Type is application/json
NSError *error;
NSDictionary *parsedData = [parser objectFromString:apiResponse error:&error];
if (parsedData == nil) {
NSLog(@"ERROR parsing api response with RestKit...%@", error);
return;
}
[RKObjectMapping addDefaultDateFormatterForString:@"yyyy-MM-dd'T'HH:mm:ssZ" inTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; // This is handy in case you return dates with different formats that aren't understood by the date parser
RKObjectMappingProvider *provider = [RKObjectMappingProvider new];
// This is the error mapping provider that RestKit understands natively (I copied this verbatim from the RestKit internals ... so just go with it
// This also shows how to map without blocks
RKObjectMapping* errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping mapKeyPath:@"" toAttribute:@"errorMessage"];
[provider setMapping:errorMapping forKeyPath:@"error"];
[provider setMapping:errorMapping forKeyPath:@"errors"];
// This shows you how to map with blocks
RKObjectMapping *teacherMapping = [RKObjectMapping mappingForClass:[Teacher class] block:^(RKObjectMapping *mapping) {
[mapping mapKeyPath:@"id" toAttribute:@"objectId"];
[mapping mapKeyPath:@"name" toAttribute:@"name"];
}];
[provider setMapping:teacherMapping forKeyPath:@"teacher"];
RKObjectMapper *mapper = [RKObjectMapper mapperWithObject:parsedData mappingProvider:provider];
Teacher *teacher = nil;
RKObjectMappingResult *mappingResult = [mapper performMapping];
teacher = [mappingResult asObject];
NSLog(@"Teacher is %@ with id %lld and name %@", teacher, teacher.objectId, teacher.name);
}
我的回調可以很明顯的重構這使它更清潔的,但現在解決了我所有的問題..沒有更多的解析......只是響應 - >魔法 - >對象
你的問題是可以理解的,沒有後顧之憂:) – tekknolagi
哈哈,謝謝:) –
看一看這個鏈接[JSON反對] [1] [1]:HTTP://計算器。 com/questions/5645703/how-to-convert-json-data-to-objects-in-iphone – mH16