這是我的post.json文件:JSONKit給予解析錯誤,但JSONLint.org說,這是有效的
[
{
"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文件的代碼:即從JSONKit objectWithData打印出來
- (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;
}
錯誤:
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失敗對我而不是搶。
是否」 - 在JSONLint上驗證時可能有一些隱藏/控制/特殊字符(例如BOM)未被複制? – 2013-03-27 01:34:15
將「數據」轉換爲字符串並記錄下來,以確保它在那裏。 – 2013-03-27 16:08:45