0
我不能相信我不能让连这个简单的测试存根与JSONKit工作...JSONKit - objectFromJSONData似乎总是返回nil
我有一个包含一个有效的JSON文件JSON文件,而这个简单的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSError *err = nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"dk" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&err];
NSString *dkk = [[NSString alloc] initWithContentsOfFile:filePath];
NSLog(@"Found file with contents: \n%@",dkk);
if(jsonData)
{
NSLog(@"Data length is: %d",[jsonData length]);
id objectReturnedFromJSON = [jsonData objectFromJSONData];
if(objectReturnedFromJSON)
{
if([objectReturnedFromJSON isKindOfClass:[NSDictionary class]])
{
NSDictionary * dictionaryFromJSON = (NSDictionary *)objectReturnedFromJSON;
// ...
} else {
NSLog(@"no dictionary from the data returned by the server... check the data to see if it's valid JSON");
}
} else {
NSLog(@"nothing valid returned from the server...");
}
} else {
NSLog(@"no data back from the server");
}
}
objectFromJSONData的结果似乎总是返回null。输出如下所示:
2013-01-15 00:58:37.187 JSONTest[38110:c07] Found file with contents:
myObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"favorites": {
"color": "Blue",
"sport": "Soccer",
"food": "Spaghetti"
}
}
2013-01-15 00:58:51.818 JSONTest[38110:c07] Data length is: 196
2013-01-15 00:58:54.058 JSONTest[38110:c07] Data length is: (null)
(lldb)
我错过了什么?输入的JSON是有效的(如NSLog语句所示)。
对不起,如果这是愚蠢的,但即使在这个小小的测试案例上,我也浪费了太多时间,让我觉得我错过了一些东西。
把你的JSON [这里](http://json.parser.online.fr/)并验证它。 'myOjbect'似乎不属于你的JSON的一部分。 –