Iphone JSONKit给出了解析错误,但是JSONLint.org说它';这是有效的
这是我的post.json文件:Iphone JSONKit给出了解析错误,但是JSONLint.org说它';这是有效的,iphone,ios,json,cocoa-touch,jsonkit,Iphone,Ios,Json,Cocoa Touch,Jsonkit,这是我的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
[
{
"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却没有。正如pst所指出的,问题在于。在Xcode中,如果右键单击文件名,选择“打开为”并选择“十六进制”,您将看到: 前三个字符显然不是标准文本字符。幸运的是,您可以在Xcode的十六进制编辑器中突出显示这三个字符,删除它们,保存文件,现在应该可以修复它了
原始答复: 另外,您是否确定JSON包含在您的捆绑包中(请在“目标设置”的“构建阶段”中检查“复制捆绑包资源”)。我刚刚使用Cocoa标准JSON解析类,
NSJSONSerialization
,解析了您的JSON,没有任何问题。或许您应该尝试检查数据,确保一切正常:
NSLog(@"data=%@", [[[NSString alloc] initWithData:data] autorelease]);
但是我用JSONKit
和NSJSONSerialization
解析了您的JSON,没有发生任何意外
NSString *filename = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filename];
if (!data)
{
NSLog(@"%s: data was nil", __FUNCTION__);
return;
}
JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
NSError *error = nil;
id results = [decoder objectWithData:data error:&error];
// Also tested with NSJSONSerialization
//
// id results = [NSJSONSerialization JSONObjectWithData:data
// options:0
// error:&error];
if (!error)
NSLog(@"%s: results = %@", __FUNCTION__, results);
else
NSLog(@"%s: error = %@", __FUNCTION__, error);
正如pst所指出的,问题在于。在Xcode中,如果右键单击文件名,选择“打开为”并选择“十六进制”,您将看到:
前三个字符显然不是标准的文本字符。幸运的是,您可以在Xcode的十六进制编辑器中突出显示这三个字符,删除它们,保存文件,现在应该可以修复它了
原始答复:
另外,您是否确定JSON包含在捆绑包中(请在“目标设置”的“构建阶段”中选中“复制捆绑包资源”)。我只是使用Cocoa标准JSON解析类,NSJSONSerialization
解析了您的JSON,没有发生意外。也许您应该检查数据
,确保一切正常:
NSLog(@"data=%@", [[[NSString alloc] initWithData:data] autorelease]);
但是我用JSONKit
和NSJSONSerialization
解析了您的JSON,没有发生任何意外
NSString *filename = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filename];
if (!data)
{
NSLog(@"%s: data was nil", __FUNCTION__);
return;
}
JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
NSError *error = nil;
id results = [decoder objectWithData:data error:&error];
// Also tested with NSJSONSerialization
//
// id results = [NSJSONSerialization JSONObjectWithData:data
// options:0
// error:&error];
if (!error)
NSLog(@"%s: results = %@", __FUNCTION__, results);
else
NSLog(@"%s: error = %@", __FUNCTION__, error);
Was“”-在JSONLint上进行验证时,可能有一些隐藏/控制/特殊字符(如BOM)未被复制?将“数据”转换为字符串并记录,以确保其全部存在。Was“”-在JSONLint上进行验证时,可能有一些隐藏/控制/特殊字符(如BOM)未被复制?将“数据”转换为字符串并记录,可以肯定的是,一切都在那里。JSONKit非常清楚地表明,您必须严格遵守JSON的规则,而NSJSONSerialization
则更加荒唐可笑。@borrden我刚刚用JSONKit
试用了提供的JSON,效果很好。就我个人而言,我希望看到Mark在完成进一步诊断之前验证数据是否已成功加载。添加上面的pic以显示我正在测试的.json文件已添加到目标构建阶段“复制捆绑资源”中,还添加了if(!data)上的测试Rob解决了这个问题,基本上在文件的开头有额外的字符,这些字符是看不见的,但是在从空文件模板创建文件并粘贴json时,Xcode添加了这些字符。教训是,如果可能的话,在另一个编辑器中创建文本文件。JSONKit非常清楚,您必须严格遵守JSON的规则,而NSJSONSerialization
则更为愚蠢。@borrrden我刚刚用JSONKit
尝试了提供的JSON,效果很好。就我个人而言,我希望看到Mark在完成进一步诊断之前验证数据是否已成功加载。添加上面的pic以显示我正在测试的.json文件已添加到目标构建阶段“复制捆绑资源”中,还添加了if(!data)上的测试Rob解决了这个问题,基本上在文件的开头有额外的字符,这些字符是看不见的,但是在从空文件模板创建文件并粘贴json时,Xcode添加了这些字符。如果可能的话,在另一个编辑器中创建文本文件。