Derek Saunders Derek Saunders - 8 months ago 40
iOS Question

New to JSON API how to access the values in objective-c?

Below is my code to access the JSON API from, this works perfectly to access the information I am just having trouble with accessing the key, value pairs.

NSURL *equipmentURL = [NSURL URLWithString: [NSString stringWithFormat:@"", self.carID, apiKey]];

NSData *jsonData = [NSData dataWithContentsOfURL:equipmentURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

self.engineArray = [NSMutableArray array];

NSArray *equipmentArray = [dataDictionary objectForKey:@"equipment"];

for (NSDictionary *carInfoDictionary in equipmentArray) {

NSArray *attributes = [carInfoDictionary objectForKey:@"attributes"];
NSLog(@"%@", attributes);


In the NSLog from the above code shows this:

2016-11-03 10:21:26.029 CarWise[25766:1896339] (
name = "Engine Immobilizer";
value = "engine immobilizer";
name = "Power Door Locks";
value = "hands-free entry";
name = "Anti Theft Alarm System";
value = "remote anti-theft alarm system";

My main question is how can I access the name and value for each array? Let's say I want to create a UILabel that will have the string of one of the values?


Probably this will help

// Array as per the post

NSArray *attributes = (NSArray *)[carInfoDictionary objectForKey:@"attributes"];

// Loop to iterate over the array of objects(Dictionary)

for (int i = 0; i < attributes.count; i++) {
    NSDictionary * dataObject = [NSDictionary dictionaryWithDictionary:(NSDictionary *)attributes[i]];

    // This is the value for key "Name"
    NSString *nameData = [NSString stringWithString:[dataObject valueForKey:@"name"]];

    NSLog(@"Value of key : (name) : %@", nameData);