0

Hi I am beginner in ios in my project i have tried to get below Json Array data but it's showing exception(like -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7be53f80) what did i do here wrong please help me, and here data is successfully loaded in my main arrayList

my json formate:-

  final respone dictionary(
        {
        Name = " ";
        id = 0;
    },
        {
        Name = "PREMIER DIM OUT";
        id = 10;
    },
        {
        Name = "PRADO COLLECTION";
        id = 15;
    },
        {
        Name = "PURE WALLS 6";
        id = 16;
    },
        {
        Name = PLATINA;
        id = 17;
    },
        {
        Name = "SARMASIK LEATHER";
        id = 19;
    },
        {
        Name = "PICASA COLLECTION";
        id = 21;
    },
        {
        Name = "3PASS BLACK OUT-SLOT LOT";
        id = 25;
    },
        {
        Name = ABYSS;
        id = 26;
    },
        {
        Name = ABYSSION;
        id = 27;
    },
        {
        Name = "ACC BOOK MODERN ELEMENT 2";
        id = 28;
    },
        {
        Name = "ACCESSORIES ROUSES 342";
        id = 29;
    },
        {
        Name = ACE;
        id = 30;
    },
        {
        Name = "ACHILLES (PAZZION)";
        id = 31;
    }
  )  
    my code:-
    ------------

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

        NSString * allDataDictionbary = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];

      NSDictionary * responseString = [allDataDictionbary JSONValue];

      NSLog(@"final respone dictionary%@",responseString);

     NSMutableArray * mainArray = [[NSMutableArray alloc]init];
         [mainArray addObject:responseString];

         for (int i = 0; i<mainArray.count; i++) {


                NSDictionary *BatchDict = [mainArray objectAtIndex:i];

                NSString * name = [BatchDict objectForKey:@"Name"];
                NSString * Id = [BatchDict objectForKey:@"id"];

                [NameArray addObject:name];
                [IdArray addObject:Id];
            }


            NSLog(@"so finally name array%@",NameArray);
            NSLog(@"so finally Id array%@",IdArray);
    }
9
  • What is your "mainDictyionary" ? Ramji please put some more code.. Commented Dec 9, 2015 at 13:04
  • 1
    i have added mainDictinary in my mainArray Commented Dec 9, 2015 at 13:05
  • 1
    That is an array, not a Dictionary.. Commented Dec 9, 2015 at 13:07
  • 1
    mainDictyionary is my response dictionary from services and i have added that in MutableArray(mainarray) Commented Dec 9, 2015 at 13:08
  • 1
    you mean what should i post? Commented Dec 9, 2015 at 13:12

3 Answers 3

3

You are making a mistake of taking an array as dictionary. You are adding an array into array and you are treating this added array as a dictionary.

You need to only enumerate through your JSON array object..

If still not getting, post the full response, I will help you !!!

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

Comments

2

By seeing your JSON FROMAT.It is clear that is Array.So Please put your response json in to Array your other code is right.

Just Follow this

NSArray *jsonArray = YOUR_JSON_RESPONSE_AS_SHOWN_ABOVE
NSMutableArray * mainArray = [[NSMutableArray alloc] initWithArray:jsonArray];

for (int i = 0; i<mainArray.count; i++) {


    NSDictionary *BatchDict = [mainArray objectAtIndex:i];

    NSString * name = [BatchDict objectForKey:@"Name"];
    NSString * Ids = [BatchDict objectForKey:@"id"];

    [NameArray addObject:name];
    [IdArray addObject:Ids];
}

NSLog(@"so finally name array%@",NameArray);
NSLog(@"so finally Id array%@",IdArray);

It will work fine . . . .

1 Comment

Abhi use this. . .Jus put your above response in jsonArray object and run this code . . .
1

All you have to do is to put JSONdata into dictionary.

For example like this:

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data 
                                                           options:kNilOptions 
                                                             error:&error]; 

where data is your JSON object, then you can simply use

NSLog(@"Dictionary: %@", [dictionary description]);

Hope this will work for you.

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.