2013-04-05 100 views
0

我正在使用Objective-C併爲iPhone開發。如何獲取FBRequest requestForMyFriends作爲JSON格式文件的結果?

我送了一個登錄的用戶的朋友的請求,並且將數據寫入到磁盤文件,像這樣:

[[FBRequest requestForMyFriends] startWithCompletionHandler:^(FBRequestConnection *connection, 
                     NSDictionary *results, 
                     NSError *error) 
{ 
if(!error){ 

NSDictionary* friends = results; 

NSLog(@"Found %i friends", friends.count); 

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docDir = [paths objectAtIndex:0]; 
NSString* fileName = [docDir stringByAppendingPathComponent:@"userInfo.dat"]; 

if ([friends writeToFile:fileName atomically:YES]) { 
    NSLog(@"Wrote friends file to disk!"); 
    sendFBEvent(FB_EVENT_DETAILS_FRIENDS, [fileName UTF8String]); 
} 
else{ 
    NSLog(@"Couldn't write friends details to disk!"); 
} 

這工作,但檢索並寫入到磁盤上的文件是一個XML文件。我幾乎可以肯定的是,在一個透明的版本中,我能夠以JSON格式保存文件,但是後來我失去了該版本,並且不記得我是如何實現它的。

如果有人能告訴我如何以JSON格式文件的形式保存此文件,我將不勝感激?我之前用一種非常簡單的方法完成了它,這與我目前的方法沒有多大區別,但如果我能記住的話,我會被詛咒的。

回答

0

爾加,非常抱歉,但我正在尋找的Facebook相關的結果時,得到的答案是什麼,原產於目標C:

Objective-C/iOS: Converting an array of objects to JSON string

原來可以使用

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:contactsToBeSynced options:NSJSONWritingPrettyPrinted error:&error]; 

要轉換數組從請求中拉入JSON。對不起,希望至少對未來有用!