1

I have a JSON array(dictionary?) of objects that are themselves an array. I need to find a value within one of these arrays so that I can compare it later. Part of my JSON data:

[
  {
    "Name": "Exhibitor",
    "Url": "api/congress/exhibitor",
    "ResourceType": "Data",
    "LastMod": 1389106977
  },
  {
    "Name": "Workshop",
    "Url": "api/congress/workshop",
    "ResourceType": "Data",
    "LastMod": 1389106977
  },
  {
    "Name": "Speaker",
    "Url": "api/congress/Speaker",
    "ResourceType": "Data",
    "LastMod": 1389106977
  },
]

My method receives a table name as a parameter and returns a time stamp. How would I receive the time stamp (1389106977) for the table "workshop" for example? This seems so simple but I cannot work it out for 'nested' arrays/dictionaries.

Thanks,

edit:

This is the my code with trojanfoe's added to it.

NSError* localError;
NSMutableArray *syncDataArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (syncDataArray)
{
    NSNumber *lastMod = nil;
    for (NSDictionary *dict in syncDataArray) 
    {
        NSLog(@"current table is: %@", dict[@"Name"]);
        if ([tableName isEqualToString:dict[@"Name"]]) 
        {
            lastMod = dict[@"LastMod"];
            //break;
         }
    }
    NSLog(@"LastMod = %@", lastMod);
}
else
{
    NSLog(@"syncDataArray is empty");
}

This works perfectly and makes sense

2
  • What JSON de-serializer are you using? Commented Jan 10, 2014 at 13:54
  • What you have is an array of JSON "objects" (dictionaries). Go to json.org and study the notation so you'll understand it. Commented Jan 18, 2014 at 13:49

3 Answers 3

2

The JSON data looks like an array of dictionaries, so you can iterate over the array and test for the "Name" entry:

NSArray *jsonData = ...;   // You have converted JSON to Objective-C objects already
NSNumber *lastMod = nul;
for (NSDictionary *dict in jsonData) {
    if ([@"Workshop" isEqualToString:dict[@"Name"]]) {
        lastMod = dict[@"LastMod"];
        break;
    }
}
if (lastMod) {
    // You found it
}

(Note I am not certain the type of object used to store the "LastMod" object, so you might need to do some debugging to find out).

EDIT If you make extensive use of this data you should immediately convert the JSON data into an array of (custom) model objects, which will make it easier to manipulate the data as your app becomes more complex.

Sign up to request clarification or add additional context in comments.

1 Comment

"LastMod" will return a value that is an NSNumber.
0

You have an array for dictionaries so it would look something like :

NSNumber *timestamp = [[JSON objectAtIndex:index] objectForKey:@"LastMod"];

Comments

0
NSNumber *timestamp = response[1][@"LastMod"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.