0

I need code (example) to show how can I iterate a JSON return array of dictionaries that contains nested dictionaries as a result of a join query. For example I get the following returned (ignore syntax structure for the purpose of this example): NSArray{PairofValues:Key:X;Value:{{PairofValues:Key:X;Value:NSDictionary}{PairofValues:Key:X;Value:Y} Where X is the SalesForce object and Y is the value returned for the object, how can I then go through the JSON and use the object at index method to go through each part so that I can get the values for each key, including the nested dictionary.

0

Let's suppose you have array containing NSDictionary:

NSArray *array; //suppose:

First method:

for (id nsdict in array) {
    // do something with object "nsdict"
    for (id key in nsdict) {
        NSLog(@"key: %@   value:%@", key, [nsdict objectForKey:key]);
    }
}

Second Method:

NSEnumerator *nse = [array objectEnumerator];
id nsdict; //
while (nsdict= [nse nextObject]) {
  // do something with object
  for (id key in nsdict) {
            NSLog(@"key: %@   value:%@", key, [nsdict objectForKey:key]);
        }
}
0

It is quite easy to do: The JSON returns an array of objects or dictionaries, the dictionaries contain themselves contain dictionaries. So you set the JSON return to an NSarray, let's call it arrayJSONresult. You can then use objectAtIndex method to take the particular NSDictionary object in the array at that index point and put it into another NSdictionary object that you create, call it dictionaryParentRecord. Now that you have a dictionary and you know that it contains another dictionary, you then create another NSDictionary object and place that nested dictionary with the key into the child dictionary. You can then interrogate each of the objects, using the objectforkey method, to retrieve their values (you need to refer to the value by the key of the object). Below is some sample code that compiles a string from the values within the dictionaries and pass it into the cell text:

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
NSDictionary *dictionaryVisitDetails = [_arrayCustomerVisits objectAtIndex:indexPath.row];
NSDictionary *objectAccountDetails = [dictionaryVisitDetails objectForKey:@"Account"];
NSMutableString *stringCellText = [[NSMutableString alloc]init];
NSString *stringGapType1=@" ";
NSString *stringGapType2=@" : ";
NSString *stringGapType3=@", ";
NSString *stringCellTextPart1 = [dictionaryVisitDetails objectForKey:@"ActivityDate"];
NSString *stringCellTextPart2 = [dictionaryVisitDetails objectForKey:@"Time_Slot__c"];
NSString *stringCellTextPart3 =[objectAccountDetails objectForKey:@"Name"];
NSString *stringCellTextPart4 =[objectAccountDetails objectForKey:@"Full_Address__c"];
[stringCellText appendString:stringCellTextPart1];
[stringCellText appendString:stringGapType1];
[stringCellText appendString:stringCellTextPart2];
[stringCellText appendString:stringGapType2];
[stringCellText appendString:stringCellTextPart3];
[stringCellText appendString:stringGapType3];
[stringCellText appendString:stringCellTextPart4];
cell.textLabel.text = stringCellText;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.