views:

103

answers:

3

Currently I am using the following code to parse the JSON link sent. This is how I also send a GET call to the Google Reader API for an upcoming iPhone application of mine.

- (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];
}

I can successfully parse the JSON using the above code, and it successfully outputs the titles into NSLog. In my RootViewController.m, I call the following to grab this -(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
}

The code above successfully works with the credentials entered. As you can see there is also a commented NSString called feedTitle. This is where I want to pull the @"title" from the parsed JSON but I do not know how to call it.

Any help would be greatly appreciated!

This is what the JSON source looks like:

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

I'm interested in only the "title" node.

+2  A: 

Well, it would help if you added the source JSON but it's quite easy to grasp how SBJSON parses incoming JSON.

Just an example:

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

This JSON String would be parsed so you can access it by using this code

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

Edit: Checked my personal Google Reader feed:

The JSON looks like this

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

So the corresponding Objective C Code would be:

NSArray* subscriptions= [feeds valueForKey:@"subscriptions"]);
foreach(NSDictionary* item in subscriptions) {
    // Do stuff 
    // NSString* title = [item valueForKey:@"title"]
    // NSString* id = [item valueForKey:@"id"]
}
Henrik P. Hessel
Okay, I can easily parse the JSON, however my main need is to actually allocate the title of the feed to a UITableViewCell's textLabel.text.
the0rkus
that's quite easy, what's the problem? just use the subscriptions array as your datasource.
Henrik P. Hessel
Being brutally honest, I have no idea how to do so. I'm only knew to JSON so I'm a little confused.
the0rkus
That's another question :) Post it here (but it's already answered many times here and on the web). Did I answer your question: Please accept it :)
Henrik P. Hessel
Accepted it :) you able to link me to something that would aid me in the appropriate way of using the subscriptions array as my datasource? I'm in dire need of this working, everything else I can do as long as I understand how to delegate these datasources from the appropriate array.
the0rkus
Yap, got some minutes to spare. Just contact me via IM (ICQ) or email (see my profile)
Henrik P. Hessel
I emailed you :)
the0rkus
A: 

I'm not sure I understand the question. Are you trying to get a title for the feed as a whole, or per-item? Because I can't see a title property for the subscriptions array in the source JSON.

Henry Cooke
Added the JSON source :)
the0rkus
A: 

Hi, can please describe the [self requestSession], thanks.

akash