IPhone, synchronous and asynchronous JSON Parse

31 Jan

A common need in Iphone or mobile applications is to get remote Data using JSON servers. In this post i will speak about the two possibilies : Synchronous and Asynchronous calls and how to deserialise JSON Data with JSON Framework for Objective-C.

So you will need to import the class files of the JSON Framework into your project first.

Synchronous Call

responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/json"]];
NSURLResponse *response = nil;
NSError *error = nil;
//getting the data
NSData *newData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//json parse
NSString *responseString = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [responseString JSONValue];
//Accessing JSON content
NSLog(@"type :  %@", [jsonObject objectForKey:@"Type"] );

Some explanations :

- response is the url response for the json call

- error is error returned by NSURLConnection

- newData is data returned by the server

Notice that you can cast the JSON deserialized data into a NSDictionnary or an NSMutuableArray.

ASynchronous Call

To make an Asynchronous call we’ll need to use NSURLConnection with initWithRequest method, then we need to implement differents other methods to handle the finish of data loading, error handling etc.

//in the viewDidLoad
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost/json"]];//asynchronous call
[[NSURLConnection alloc] initWithRequest:request delegate:self];

Implementing delegate methods :

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [responseData release];
    NSDictionary *jsonObject = [responseString JSONValue];
    NSLog(@"type :  %@", [jsonObject objectForKey:@"Type"] );
    [connection release];
}
Be Sociable, Share!

Comments

Powered by Facebook Comments

Be Sociable, Share!

5 Responses to “IPhone, synchronous and asynchronous JSON Parse”

  1. Philip DudleyNo Gravatar April 8, 2011 at 07:14 #

    Just what I was looking for! Great reference, thank you.

  2. MattNo Gravatar August 1, 2011 at 18:13 #

    I was having this problem at my work for several hours, and I wanted to let you know that a quick google search helped me get past it in no time! Thank you very much(from both myself and my boss ;)

  3. ChrisNo Gravatar August 11, 2011 at 17:49 #

    @Phillip
    I’m glad you understand this. To be honest, it’s a little above my head. I’ve been doing iPhone apps for a while now and getting remote data would be helpful. Oh well, back to my search.

  4. ChookNo Gravatar October 11, 2011 at 16:39 #

    Is it possible to be able to lionk to the framework rather than importing the source?

    • adminNo Gravatar October 11, 2011 at 16:54 #

      sincerely i don’t know if it’s possible to do that .. sorry :)

Leave a Reply