0

I have managed to extract the following array (which I am dumping to console) from some json. How can I get and print out the value for one of the elements, i.e. task?

Objective-C:

NSArray *array = [dict objectForKey:@"row"];
        NSLog(@"array is: %@",array);

Console output:

array is: {
    0 = 1;
    1 = "send email";
    2 = "with attachment";
    ltask = "with attachment";
    task = "send email";
    userid = 1;
}

4 Answers 4

3

array looks like it is actually an NSDictionary, so reference the key to get the value for it.

NSLog(@"Task: %@", array[@"task"]);
Sign up to request clarification or add additional context in comments.

5 Comments

the above objective c prints out the output shown in the console so I think it is valid. How would I capture and print out just the value of task i.e. "send email".
My line of code should print out "send email". But if you wanted to use a variable for it, then you could do id value = array[@"task"]; and then print value.
When I put in the code in answer, I get red error "Expected method to print dictionary element not found on object of type 'NSArray'" Same error with id value
NSArray will print to the console using parenthesis (), whereas NSDictionary will print to the console with curly braces{}. The curly braces in your console output tell me that array is an NSDictionary. Can array be either? If so, then you will need to use the isKindOfClass: method to handle the different types correctly.
Ok. You were right, it was not an array, it was a dictionary. However I was calling it an array with NSArray *array = ... which seems to have been source of error. Am up voting your answer and accepting one below by Subbu which caught conflict.
0

the variable array doesn't seem to be NSArray . Does this work for you?

    id  array = [dict objectForKey:@"row"];

    if([array isKindOfClass:[NSDictionary class]]){
        NSLog(@"Value of task %@",array[@"task"]);
    }

1 Comment

Yes that did trick thanks. Original code for parsing came from different json that did start with an array but in this case, it was a dictionary. Thanks.
0

From the log, it looks like the output is an NSDictionary object, so to get the value of task key just do this

NSDictionary *myDict = dict[@"row"];
NSString *task = myDict[@"task"];
NSLog(@"task = %@", task);

if you want to confirm just check the class type using isKindOfClass: method

if([dict[@"row"] isKindOfClass:[NSDictionary class]]) {

    NSDictionary *myDict = dict[@"row"];
    NSString *task = myDict[@"task"];
    NSLog(@"task = %@", task);

} else if([dict[@"row"] isKindOfClass:[NSArray class]]) {

    NSArray *myArray = dict[@"row"];
    NSDictionary *myDict = myArray[0];
    NSString *task = myDict[@"task"];  
    NSLog(@"task = %@", task);

}

Comments

0

try

        if ([[dictionary allKeys] containsObject:@"row"]) {
            NSObject *objRow = dictionary[@"row"];
            if(objRow){
                if([objRow isKindOfClass:[NSArray class]]){
                     NSArray *arr = (NSArray *)objRow;
                     ....
                }
                if([objRow isKindOfClass:[NSDictionary class]]){
                     NSDictionary *dic = (NSDictionary *)objRow;
                     ....
                }
            }
        }

Comments