Simon Simon - 5 months ago 18
JSON Question

iOS NSDictionary accessing a JSON value

How do I access the value in "display_name", assuming this is an NSDictionary returned from a JSON get request?

This is what I have tried so far:

NSString *carName = vehicleListDict[@"response:display_name"];
NSString *carName = vehicleListDict[@"display_name"];

{
"response": [
{
"color": null,
"display_name": "hello",
"id": 321,
"option_codes": "MS01,RENA,TM00,DRLH,PF00,BT85,PBCW,RFPO,WT19,IBMB,IDPB,TR00,SU01,SC01,TP01,AU01,CH00,HP00,PA00,PS00,AD02,X020,X025,X001,X003,X007,X011,X013",
"user_id": 123,
"vehicle_id": 1234567890,
"vin": "5YJSA1CN5CFP01657",
"tokens": [
"x",
"x"
],
"state": "online"
}
],
"count": 1
}

Answer Source

In you response you can see that the response contains a array if item, so lets get the array:

NSArray *response = vehicleListDict[@"response"];

Then we can the value like:

NSString *displayName = nil;
if (response.count > 0) {
    NSDictionary *vehicle = response[0];
    displayName = vehicle[@"display_name"];
}