这是我的 post.json 文件:
[
{
"Title": "Introduction to WCF",
"Url": "http://myaddress/videos/introduction-to-wcf",
"Thumbnail": "http://myaddress/images/20110212_01.jpg",
"Exceprt": "Introduction to WCF",
"PostDate": "2011-02-12T14:26:07",
"Id": 39,
"Mp4Video": "http://myaddress/2012/05/20110212_01.mp4",
"Speakers": [
{
"Name": "Mark Wilkinson",
"Slug": "mark-wilkinson"
}
],
"Groups": [
{
"Name": "C# UG",
"Slug": "cs-ug"
}
],
"Tags": [
{
"Name": "WCF Services",
"Slug": "wcf-services"
}
]
}
]
在 jsonlint.org 中发布它并验证。
这是我一直用于其他有效的 JSON 文件的代码:
- (void)test_can_read_from_groups_file_and_build_JSONDictionary {
id result = [self data_from_JSON_file:@"post"];
[Assert isNotNil:result]; // is returning as nil, so test is failing
}
- (id)data_from_JSON_file:(NSString *)fileName {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *jsonString = [bundle pathForResource:fileName ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonString];
JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
NSError *error = nil;
id result = [decoder objectWithData:data error:&error];
if (error) {
NSLog(@"*********\r\r\r\r\r\r\r Error was: %@", [error localizedDescription]);
}
return result;
}
从 JSONKit objectWithData 打印出的错误:
Error was: Unexpected token, wanted '{', '}', '[', ']', ',', ':', 'true', 'false', 'null', '"STRING"', 'NUMBER'.
ETA:是的,它处于构建阶段:
添加:
if (!data)
{
NSLog(@"\r\r\r\r\r\r%s: data was nil", __FUNCTION__);
return nil;
}
它没有触及这个分支,所以数据不是零。
使用 JSONKit 解码器更改为:
id results = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
并且它有效,仍然对为什么 JSONKit 对我失败但对 Rob 失败感到困惑。