【发布时间】:2011-06-08 17:37:43
【问题描述】:
我正在尝试解码一个 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];
提前致谢。
【问题讨论】:
标签: objective-c json