Last Updated: November 19, 2020
· anthonylevings

Converting JSON into an Objective-C object (Xcode for iOS)

// Retrieve local JSON file called example.json

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"];

 // Load the file into an NSData object called JSONData 

NSError *error = nil;

NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];

 // Create an Objective-C object from JSON Data

id JSONObject = [NSJSONSerialization

Finally apply introspection to the object to find out whether it is a dictionary, array, etc. and to then make use of it accordingly - see

If you want to retrieve JSON from a URL or roll your own JSON inside your app, then see the full post here:

Related protips:

Installing Xcode Command Line Tools on OS X Mavericks

2 Responses
Add your response

Simple and fast, thank's!

over 1 year ago ·

NSString *converEnvoi =[nEnvoi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:converEnvoi] encoding:NSUTF8StringEncoding error:NULL];

NSString *new = [result stringByReplacingOccurrencesOfString: @"[" withString:@""];
result = [new stringByReplacingOccurrencesOfString: @"]" withString:@""];
if (result)
    NSLog(@"LOC RESULT: %@", result);
    NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &error];

    NSArray *resultsArray = [resultDict objectForKey:@"iddep"];

    if(resultsArray.count > 0)


RESULT: {"iduti":"142","iddep":"25"}

Bug : NSArray *resultsArray = [resultDict objectForKey:@"iddep"];

Merci d'avance

over 1 year ago ·