Я пытаюсь декодировать файл json и получать тип значения для каждого не зная имен атрибутов или порядка.
Т.е..
{
"Name": "Klove",
"Altitude": "100",
"Latitude": "43.421985",
"Longitude": "-5.823897"
}
Итак: Имя будет NString с Klove внутри. Широта поплавка с 43.421985 ....
//File initialization
NSString *filePath = [[NSBundle mainBundle]
pathForResource:@"buildings" ofType:@"json"];
NSData *fileContent = [NSData dataWithContentsOfFile:filePath];
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];
NSDictionary *dict = [jsonKitDecoder objectWithData:fileContent];
// the party starts
for (NSDictionary *poi in dict) {
for (int i = 0; i <= [[poi allKeys]count]-1 ; i++) {
// Here i'm trying to get the object type (but never with success)
if([[poi objectForKey:[[poi allKeys]objectAtIndex:i]]
isKindOfClass:[NSNumber class]]
|| [[poi objectForKey:[[poi allKeys]objectAtIndex:i]]
isKindOfClass:[NSArray class]]
|| [[poi objectForKey:[[poi allKeys]objectAtIndex:i]]
isKindOfClass:[NSDictionary class]])
{
NSLog(@"Other kind of class detected");
// do somthing but never happens
// in fact, if i use [[poi objectForKey:[[poi allKeys]objectAtIndex:i]class]
// always is __NSCFString
}
NSLog(@"%@",[[poi allKeys]objectAtIndex:i]);
NSLog(@"%@",[poi objectForKey:[[poi allKeys]objectAtIndex:i]]);
}
}
Итак ... я не знаю как я могу получить тип объекта в словаре json . Любая идея? Если это возможно, я бы не хотел делать такие проверки, как:
if ([poi allKeys]objectAtIndex:i] isEqualToString(@"Latitude"))
[[poi objectForKey:i]floatValue];
Заранее спасибо.