現在、送信されたJSONリンクを解析するために次のコードを使用しています。これは、私の今後のiPhoneアプリケーションのためにGoogleReaderAPIに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から@ "title"を取得したいところですが、それを呼び出す方法がわかりません。
どんな助けでも大歓迎です!
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":""}
]
}
「タイトル」ノードだけに興味があります。