JSON parsing using NSJSONSerialization

suggest change
NSError *e = nil;
NSString *jsonString = @"[{\"id\": \"1\", \"name\":\"sam\"}]";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options:  NSJSONReadingMutableContainers error: &e];

if (!jsonArray) {
    NSLog(@"Error parsing JSON: %@", e);
} else {
    for(NSDictionary *item in jsonArray) {
        NSLog(@"Item: %@", item);
    }
}
Item: {
    id = 1;
    name = sam;
}

Example 2: using the contents of a URL

dataWithContentsOfURL: takes an NSURL, not an NSString. This call is synchronous and blocks the current thread.

NSURL *url = [NSURL URLWithString:@"https://example.com/data.json"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"json: %@", json);

Sample response:

json: {
    MESSAGE = "Test Message";
    RESPONSE =(
                {
            email = "test@gmail.com";
            id = 15;
            phone = 1234567890;
            name = Staffy;
        }
    );
    STATUS = SUCCESS;
}
NSMutableDictionary *response = [[[json objectForKey:@"RESPONSE"] objectAtIndex:0] mutableCopy];
NSString *nameStr = [response valueForKey:@"name"];
NSString *emailIdStr = [response valueForKey:@"email"];

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents