在iPhone上使用JSON Framework - 帮助!

时间:2010-10-01 03:56:02

标签: iphone objective-c nsarray google-reader json-framework

目前我使用以下代码来解析发送的JSON链接。这也是我为即将推出的iPhone应用程序向Google Reader API发送GET调用的方式。

- (NSArray *)subscriptionList
{
if(!cookies && [cookies count] == 0) {
    [self requestSession];
}

NSString * url = @"http://www.google.com/reader/api/0/subscription/list?output=json&client=scroll";

ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setRequestMethod:@"GET"];
[request setRequestCookies:cookies];
[request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"GoogleLogin auth=%@", [self auth]]];

[request startSynchronous];

subfeeds = [NSMutableArray array];

// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];

if ([request responseStatusCode] == 200) {

    NSData * sixty = [request responseData];

    NSString * body = [[NSString alloc] initWithData:sixty encoding:NSUTF8StringEncoding];
    if (body) {
        NSArray *feeds = [parser objectWithString:body error:nil];
        NSLog(@"Array Contents: %@", [feeds valueForKey:@"subscriptions"]);
        NSLog(@"Array Count: %d", [feeds count]);

        NSDictionary *results = [body JSONValue];
        NSArray *ohhai = [results valueForKey:@"subscriptions"];

        for (NSDictionary *title in ohhai) {
            subTitles = [title objectForKey:@"title"];
            NSLog(@"title is: %@",subTitles);
        }
    }
}

return subfeeds;
[subTitles release];
[parser release];
}

我可以使用上面的代码成功解析JSON,并成功将标题输出到NSLog中。在我的RootViewController.m中,我调用以下内容来获取它 - (NSArray *)subscriptionList。

-(void)viewDidAppear:animated {
GoogleReader * reader = [[GoogleReader alloc] init];
[reader setEmail:gUserString];
[reader setPassword:gPassString];

//feedItems is a NSArray where we store the subscriptionList NSArray
feedItems = [reader subscriptionList];

//NSString *feedTitle = [];

NSLog(@"%@", feedItems);

[reader release];
// the rest of the function
}

上面的代码成功使用输入的凭据。如您所见,还有一个名为feedTitle的注释NSString。这是我想从解析的JSON中提取@“标题”但我不知道如何调用它的地方。

非常感谢任何帮助!

这就是JSON源的样子:

{"subscriptions":
[
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""}
]
}

我只对“标题”节点感兴趣。

2 个答案:

答案 0 :(得分:3)

好吧,如果您添加源JSON会有所帮助,但很容易理解SBJSON如何解析传入的JSON。

只是示例

{ "myOutDict" : { "key1": "val1" , "key2" : "val2"} }

将解析此JSON字符串,以便您可以使用此代码

来访问它
NSDictionary* myOuterdict = [feeds valueForKey:@"myOutDict"]);
NSString* val1 =  [myOuterdict valueForKey:@"key1"]);
NSString* val2 =  [myOuterdict valueForKey:@"key2"]);

修改:查看我的个人Google阅读器Feed:

JSON看起来像这样

{
    "subscriptions": [{
        "id": "feed/http://adambosworth.net/feed/",
        "title": "Adam Bosworth's Weblog",
        "categories": [],
        "sortid": "0B5B845E",
        "firstitemmsec": "1243627042599"
    },
    {
        "id": "feed/http://feeds.feedburner.com/zukunftia2",
        "title": "Zukunftia",
        "categories": [],
        "sortid": "FCABF5D4",
        "firstitemmsec": "1266748722471"
    }]
}

因此相应的Objective C Code将是:

NSArray* subscriptions= [feeds valueForKey:@"subscriptions"]);
foreach(NSDictionary* item in subscriptions) {
    // Do stuff 
    // NSString* title = [item valueForKey:@"title"]
    // NSString* id = [item valueForKey:@"id"]
}

答案 1 :(得分:0)

我不确定我理解这个问题。您是要尝试获取整个Feed或每个项目的标题吗?因为我在源JSON中看不到订阅数组的title属性。