ios - JSONKit - objectFromJSONData 似乎总是返回 nil

标签 ios objective-c jsonkit

我不敢相信我什至不能得到这个与 JSONKit 一起工作的简单测试 stub ...

我有一个包含有效 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 无效。它没有根元素,myObject 键也没有括在括号中。

关于ios - JSONKit - objectFromJSONData 似乎总是返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14331947/

相关文章:

ios - 考虑到拆分的持续时间,是否可以在 iOS 上将录制的 wav 文件拆分为多个 wav 文件?

ios - 是否可以在iBeacon上写入数据

objective-c - 为 OSX 10.6 编译时找不到 NSExpression

ios - sthttprequest 和 jsonkit 与 arm64,不再工作?

ios - 重复符号 _OBJC_IVAR_$_GCOAuth.OAuthParameters ShareKit 和 RestKit。

iphone - 读取 XML 并将其显示在 TableView 中

ios - 如果我要本地化一个应用程序,我是否也应该本地化该应用程序的应用商店标题?

ios - Catalyst textview 插入符号颜色

iphone - 使用 CCSpriteFrameCache 为动画 Sprite 添加标签

iphone - JSONKit 工具上的错误