サードパーティのサービスに渡された郵便番号から返される単純な JSON 配列があります。
結果を逆シリアル化しようとすると不明なエラーが発生し、何が問題なのかわかりません。
これが私のconnectionDidFinishLoadingメソッドです。これは予想どおりに起動しますが、常に失敗します...そして、最後のelse ifでエラーが発生します。アイデア?
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading...");
self.zipCodes = [NSMutableArray array];
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil) {
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Deserialized JSON Dictionary = %@", deserializedDictionary);
for (NSDictionary *item in jsonObject) {
NSString *city = [item objectForKey:@"adminName2"];
NSString *stateAbbreviation = [item objectForKey:@"adminCode1"];
NSString *postalCode = [item objectForKey:@"postalCode"];
NSString *distance = [item objectForKey:@"distance"];
NSString *country = [item objectForKey:@"country"];
NSString *stateName = [item objectForKey:@"stateName"];
ZipCodes *zipCode = [[ZipCodes alloc] initWithName:city stateAbbrev:stateAbbreviation postalCode:postalCode distanceFromGPSZip:distance country:country stateFullName:stateName];
[self.zipCodes addObject:zipCode];
}
}
else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Deserialized JSON Array = %@", deserializedArray);
}
else {
/* Some other object was returned. We don't know how to deal
with this situation as the deserializer returns only dictionaries or arrays */
}
}
else if (error != nil){
NSLog(@"An error happened while deserializing the JSON data.");
}
}