0

Want to extract value from the NSMutableArray

After services call I got response which is stored in NSMutableArray

`arrayList` = responseData 

The response data is an JSON string

Below is response data structure:

 {
        firstSet =     (
        );
        secondSet =     {
            id = 12;
            name = rohan;
        };
        "status_code" = 200;
        success = 1;
    }

I want to extract name field and id from the arrayList. This is what I tried, not working me.

[[[arrayList objectAtIndex:0] objectForKey:@"secondSet"] objectForKey:@"name"]];

Error Message -[__NSDictionaryM objectAtIndex:]: unrecognized selector sent to instance 0x7c6763e0

James Risner
  • 5,451
  • 11
  • 25
  • 47
kiran
  • 4,285
  • 7
  • 53
  • 98
  • 1
    possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Apr 10 '15 at 17:53
  • to solve it I need to declear NsMutableArray to NSmutablDictionary... from there i can extract the values!? – kiran Apr 10 '15 at 17:59
  • To solve you need to not assign an NSDictionary address to an NSArray pointer. – Hot Licks Apr 10 '15 at 18:02
  • @HotLicks I agree what you say, And also i got the data structure. – kiran Apr 10 '15 at 18:27

2 Answers2

1

The variable you're referring to as "arraylist" is actually a Dictionary. Try asking it for "secondset"

[[arrayList objectForKey:@"secondSet"] objectForKey:@"name"];
KirkSpaziani
  • 1,962
  • 12
  • 14
  • the variable which i defined is a NSMutableArray Not NSMutableDictionary, – kiran Apr 10 '15 at 17:57
  • Doesn't matter what it's defined as - somehow (through the JSON deserializer maybe?) the reference is pointing to a dictionary. The error indicates as such. Since all Objective-C references are just pointers, and not checked, it's easy at runtime to have something pointing to the wrong object type. – KirkSpaziani Apr 10 '15 at 18:02
  • @kiran - The running code doesn't care how you defined the pointer -- that's only meaningful to the compiler, and then only if it's right. The object type is defined by the JSON data. – Hot Licks Apr 10 '15 at 18:04
-3

you would be getting json which you may handle like this

NSDictionary *dic;
if (data!=nil)
    {
        dic = [NSJSONSerialization
               JSONObjectWithData:data //1
               options:NSJSONReadingMutableContainers
               error:&error];
    }
NSString *name = [[dic objectForKey:@"second"] objectForKey:@"name"];
Syed Ali Salman
  • 2,894
  • 4
  • 33
  • 48