Click here to Skip to main content
15,868,027 members
Articles / Mobile Apps / iOS
Tip/Trick

Parsing JSON Response from Web service to NSDictionary in iOS – Objective C

Rate me:
Please Sign up or sign in to vote.
4.71/5 (4 votes)
13 Jul 2014CPOL1 min read 34.1K   282   4   2
This tip shows how to parse JSON response received from a web service into an NSDictionary object.

Introduction

This post will show how to parse the JSON response received from a web service into an NSDictionary object. For the purpose of this post, I’ll use a sample web service hosted by jsontest.com that returns the MD5 hash of the entered sample text.

The URL of the sample web service is http://md5.jsontest.com/?text=AmoghNatu.

The output when a request is made to the above URL is as shown below:

C++
{
   "md5": "1e559716c8590f85d81abf7abff073aa",
   "original": "AmoghNatu"
}

In the sample project, I’ll display the MD5 text in an alert box.

For parsing the response JSON, we use the NSJSONSerialization class’s JSONObjectWithData method. When a request is posted to the web service, the response is initially stored in an NSData object. This NSData object is passed to JSONObjectWithData method as parameter along with json reading options and an error object, in case there is an error while parsing the JSON.

Let’s have a look at the code.

C++
- (NSDictionary *)parseJsonResponse:(NSString *)urlString
{
    NSError *error;
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error:&error];
    if (!data)
    {
        NSLog(@"Download Error: %@", error.localizedDescription);
        UIAlertView *alert =
        [[UIAlertView alloc]initWithTitle:@"Error"
                                  message:[NSString stringWithFormat:@"Error : %@",error.localizedDescription]
                                 delegate:self
                        cancelButtonTitle:@"Ok"
                        otherButtonTitles:nil];
        [alert show];
        return nil;
    }
    
    // Parsing the JSON data received from web service into an NSDictionary object
    NSDictionary *JSON =
    [NSJSONSerialization JSONObjectWithData: data
                                    options: NSJSONReadingMutableContainers
                                      error: &error];
    return JSON;
}

The method parseJsonResponse is a sample method that I have written that makes a request to the web service and gets the response in JSON. This JSON is then parsed into an NSDictionary object.

I have uploaded a sample application. The same can be downloaded from here too. Check that if required.

Hope this helps!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Lydia Gabriella16-Jul-14 10:39
Lydia Gabriella16-Jul-14 10:39 
GeneralRe: My vote of 5 Pin
Amogh Natu17-Jul-14 8:10
professionalAmogh Natu17-Jul-14 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.