Question

I'm trying to user NSJSONSerialization in a mac app, but i can't get it working, I'm trying to get it to load UserTimeline, and just show the text of the last tweet, and thats it, but I can't seem to find the proper way to use it. the api i have tried to use :

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=AlexTrott_

And

http://twitter.com/statuses/user_timeline/AlexTrott_.json

but neither of them i've had look, this is how i've been trying to use NSJSONSerialization :

NSString *tweets = [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline/AlexTrott_.json"];
    NSData *jsonData = [tweets dataUsingEncoding:NSUTF8StringEncoding];
    NSError *e = nil;
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
    NSLog(@"%@", json);

that is just what i've been using to see if it's loaded them, but it's been failing me.

I also tried was going to try this method http://www.raywenderlich.com/5492/working-with-json-in-ios-5 however thats solely for iOS and i don't know how i'd get it to work on Mac.

Any links on how to use NSJSONSerialization for mac would be brilliant, or any help with could would also be a major thanks. I tried getting help on the apple developer forums, but no luck there.

Was it helpful?

Solution

NSURL *tweets = [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline/AlexTrott_.json"];
    //NSData *jsonData = [tweets dataUsingEncoding:NSUTF8StringEncoding];
     //NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&e];
NSData *jsonData=[NSData dataWithContentsOfURL:tweets];
NSError *e = nil;

   id yourJsonData=[NSJSONSerialization JSONObjectWithData:jsonData options:
               NSJSONReadingMutableContainers error:&error];
NSLog("%@",yourJsonData );//you must get a json object here

//let's assume you need to get the key kalled user_name from your JSON Response

NSDictionary *theJson=[yourJsonData objectForKey:@"user_name"];

for(NSMutableArray *usernames in theJson){

// do something with usernames from your object
}

OTHER TIPS

My application also receives his last tweet. You should consider the limits to the server with these requests (350 per hour). If you exceed this limit, there is no nsarray in nsdata it is nsdictionary, which describes the error and the request, which will undoubtedly be taken into account. I did it this way:

NSError* error = nil;

id responseBody = [NSJSONSerialization JSONObjectWithData:data 
                                                  options:NSJSONReadingMutableContainers
                                                    error:&error];

if (error) {

    [[NSAlert alertWithError:error] runModal];
} 

if ([responseBody respondsToSelector:@selector(objectAtIndex:)]) {

    else { 

        if ([responseBody count]) {

            NSString* currentStatus = [[responseBody objectAtIndex:0] objectForKey:@"text"]; //now you can do smth with your status))
        } 
        else {

            NSLog(@"You have not tweetted yet!");
        }
    } 
} 
else if([responseBody respondsToSelector:@selector(objectForKey:)]) {

    NSString* error = [responseBody objectForKey:@"error"];
    NSLog(@"CURRENT STATUS ERROR => %@", error);        
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top