如何在控制台上打印json输出

时间:2011-06-14 09:38:51

标签: iphone json facebook-graph-api

我正在使用Facebook图形API并以json格式获取其结果。我能够打印一些结果但在其他方面感到困惑..

  application =         {
        id = 142759389130183;
        name = iphonemini;
    };
    caption = "widevision.co.in";
    "created_time" = "2011-06-14T07:56:38+0000";
    from =         {
        id = 100001507678574;
        name = "Widevision Dev";
    };
    icon = "http://www.facebook.com/images/icons/hidden.gif";
    id = "100001507678574_173203589406562";
    link = "http://widevision.co.in/";
    message = "Good Afternoon";
    name = "Check It out";
    type = link;
    "updated_time" = "2011-06-14T07:56:38+0000";
},

我可以通过此代码打印此图标,ID,链接

FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/feed" withGetVars:nil];


    NSLog(@"method called");
    //parse our json
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *facebook_response = [parser    objectWithString:fb_graph_response.htmlResponse error:nil];    

    [parser release];

    //NSString *feed;
    //  NSString *feed2;
    NSMutableArray *feed =(NSMutableArray *) [facebook_response objectForKey:@"data"];

    //  NSMutableArray *feed1=(NSMutableArray *) [feed valueForKey:@"type"];


    NSLog(@"Feed %@" ,feed );

    NSLog(@"Message is %@ ",[feed valueForKey:@"icon"]);

    NSLog(@"Name is %@",[feed valueForKey:@"name"]);

..也得到了这个

from ={
    id= ;
    name = "";
    }


NSMutableArray *streams = (NSMutableArray *)[feed valueForKey:@"from"];



    // loop over all the stream objects and print their titles
        int index;
        NSMutableDictionary *stream;

        for (index = 0; index < [feed count];index++) {
            stream = (NSMutableDictionary *)[streams objectAtIndex:index];



            NSLog(@"Message is %@:",[stream valueForKey:@"name"]);

        }

但是我如何解析这个comments = { }....

{
        application =         {
            id = 136664723060869;
            name = Iphoneapp;
        };
        caption = "bit.ly";
        comments =         {
            count = 2;
            data =             (
                                {
                    "created_time" = "2011-06-14T07:39:45+0000";
                    from =                     {
                        id = 100001507678574;
                        name = "Widevision Dev";
                    };
                    id = "100001507678574_164163733643881_1822049";
                    likes = 1;
                    message = hi;
                },
                                {
                    "created_time" = "2011-06-14T08:17:31+0000";
                    from =                     {
                        id = 100001507678574;
                        name = "Widevision Dev";
                    };
                    id = "100001507678574_164163733643881_1822143";
                    message = hmmm;
                }
            );
        };

请帮助

1 个答案:

答案 0 :(得分:0)

好吧,我在这里看到了一个清晰的json对象。我将以SBJSON为例,其中所有JSON都被解析为NSDictionary对象,其中application, caption, comments是键。注释对象再次形成一个带键的字典; count, data。一旦到达data,您就拥有了整个数据的数组,因此该数组包含多个字典。在数据数组中获取每个字典应该可以解决您的问题。祝你好运。