2

現在、送信された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":""}
]
}

「タイトル」ノードだけに興味があります。

4

2 に答える 2

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リーダーフィードをチェックしました:

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"
    }]
}

したがって、対応するObjectiveCコードは次のようになります。

NSArray* subscriptions= [feeds valueForKey:@"subscriptions"]);
foreach(NSDictionary* item in subscriptions) {
    // Do stuff 
    // NSString* title = [item valueForKey:@"title"]
    // NSString* id = [item valueForKey:@"id"]
}
于 2010-10-01T04:02:47.693 に答える
0

質問がよくわかりません。フィード全体のタイトルを取得しようとしていますか、それともアイテムごとに取得しようとしていますか?ソースJSONにサブスクリプション配列のtitleプロパティが表示されないためです。

于 2010-10-01T04:05:48.153 に答える