2013-03-27 64 views
1

這是我的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:是的,它是在構建階段:

enter image description here

添加:

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失敗對我而不是搶。

+1

是否」 - 在JSONLint上驗證時可能有一些隱藏/控制/特殊字符(例如BOM)未被複制? – 2013-03-27 01:34:15

+0

將「數據」轉換爲字符串並記錄下來,以確保它在那裏。 – 2013-03-27 16:08:45

回答

1

正如pst指出的,問題原來是BOM。在Xcode中,如果你在文件名上單擊右鍵,選擇「打開爲」,然後選擇「十六進制」,你會看到:

hex dump

這些前三個字符顯然不是標準的文本字符。幸運的是,您可以在Xcode的十六進制編輯器中突出顯示這三個字符,刪除它們,保存該文件,現在應該修復它。


原來的答覆:

另外,你確定了JSON被列入你的包(檢查「複製包資源」中的「構建階段」你的「目標設置的),我只是解析您的。JSON與可可標準JSON解析類,NSJSONSerialization,無事也許你應該嘗試檢查data並確保一切正常:

NSLog(@"data=%@", [[[NSString alloc] initWithData:data] autorelease]); 

但我既JSONKit和解析您的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); 
+0

JSONKit非常清楚,您必須嚴格遵守JSON規則,而「NSJSONSerialization」則更加鬆散 - 對此很有幫助。 – borrrden 2013-03-27 01:56:30

+1

@borrrden我只是試着用'JSONKit'提供的JSON,它工作正常。就我個人而言,我希望看到Mark在進一步診斷完成之前確認「data」已成功加載。 – Rob 2013-03-27 01:59:07

+0

添加了上面的圖片來顯示我正在測試的.json文件被添加到目標構建階段'複製包資源' – 2013-03-27 14:20:08