1

I receiving data from URL, put it in NSDictionary, and then I need to create NSArray for each tags. My NSDictionary looks like:

(
        {
        id =         {
            text = 1;
        };
        logo =         {
            text = "url 1";
        };
        name =         {
            text = "some name 1";
        };
    },
        {
        id =         {
            text = 2;
        };
        logo =         {
            text = "url 2";
        };
        name =         {
            text = "some name 2";
        };
    },
        {
        id =         {
            text = 3;
        };
        logo =         {
            text = "url 3";
        };
        name =         {
            text = "some name 3";
        };
    }   
)

I want to have array like that: arrLogo = (@"url 1", @"url 2", "url 3").

I'm doing next: arrName=[[dict objectForKey:@"name"] valueForKey:@"text"];

But Xcode gives me an error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance

Problem in the code that I'm trying to initialize an arrays, it's look like that my dict consist of arrays. How do I extract the data correctly?

daleijn
  • 718
  • 13
  • 22

2 Answers2

2

For this you have to iterate through object of Array that you have. You can use fast enumeration as

    for(object in JsonArray){

    NSString *value = [object valueForKey:@"logo"]valueForKey:@"text"]];

     //Add this value  to your array 
    }
}

Hope this helps.

Vish
  • 341
  • 1
  • 4
  • 19
  • if I have only one element in dictionary - error occured: Terminating app due to uncought exception: UnknownKeyException, reason coding-compliant for the key logo. How can O fix it? – daleijn Jan 15 '14 at 19:48
  • The exception mentions that the Key is not present in your element. Try looking your console by printing that element. – Vish Jan 16 '14 at 04:59
1

The JSONResponse you have get is NSArray not NSDictionary
You can parse you json response like this ...

NSMutableArray * tmpAry = your json response;
NSMutableArray * urlAry = [[NSMutableArray alloc] init];

for (int i=0; i<tmpAry.count; i++) {
     NSMutableDictionary * tmpDictn = [tmpAry objectAtIndex:i];
     [urlAry  addObject:[[tmpDictn objectForKey:@"logo"] valueForKey:@"text"]];
 }
NSLog(@"urlAry : %@",urlAry);

Log will display :

urlAry : (
            url 1,
            url 2,
            url 3
            )
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56