简体   繁体   English

如何获取JSON格式文件FBRequest requestForMyFriends的结果?

[英]How do I obtain results of FBRequest requestForMyFriends as a JSON formatted file?

I'm using Objective-C and developing for iPhone. 我正在使用Objective-C并正在为iPhone开发。

I'm sending a request for the friends of a logged in user and writing the data to file to disk like so: 我正在向已登录用户的朋友发送请求,并将数据写入文件到磁盘,如下所示:

[[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!");
}

This works, but the file retrieved and written to disk is an XML file. 这可行,但是检索并写入磁盘的文件是XML文件。 I am almost certain that in a pervious version I was able to save the file in a JSON format, but I have since lost that revision and can't remember how I achieved it. 我几乎可以肯定,在以前的版本中,我能够将文件保存为JSON格式,但是此后我丢失了该修订版,并且不记得我是如何实现的。

I'd appreciate it if someone could tell me how it is possible to save this file in the form of a JSON formatted file? 如果有人能告诉我如何以JSON格式保存此文件,我将不胜感激。 I've done it before with a very simple method that isn't much different from my current method but I'll be damned if I can remember how. 我以前用一种非常简单的方法完成了此操作,该方法与当前方法没有太大不同,但是如果我记得怎么做的话,我该死的。

Gah, awfully sorry but I was searching for Facebook related results when the answer was something native to Objective C: Gah,非常抱歉,但是当答案是Objective C固有的内容时,我正在搜索与Facebook相关的结果:

Objective-C / iOS: Converting an array of objects to JSON string Objective-C / iOS:将对象数组转换为JSON字符串

Turns out you can use 原来你可以使用

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

To convert the array pulled from the request into JSON. 将请求中提取的数组转换为JSON。 Sorry about that, hope this is useful to someone in future at least! 抱歉,希望至少对将来的某个人有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM