如何在ios中获取和显示Json Array数据

时间:2015-12-09 12:55:15

标签: ios objective-c json

您好我是ios的初学者在我的项目中我试图获得低于Json数组数据但是它显示异常(like -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x7be53f80)我在这里做错了什么请帮助我,这里的数据是已成功加载到我的主arrayList

我的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);
    }

3 个答案:

答案 0 :(得分:3)

你错误地将数组作为字典。您正在将数组添加到数组中,并将此添加的数组视为字典。

您只需枚举您的JSON数组对象..

如果仍然没有得到,发布完整的回复,我会帮助你!

答案 1 :(得分:2)

通过查看你的JSON FROMAT.It很清楚是Array.So请把你的响应json放到Array中,你的其他代码是对的。

请关注此

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);

它会正常工作。 。 。 。

答案 2 :(得分:1)

您所要做的就是将JSONdata放入字典中。

例如:

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

其中data是您的JSON对象,那么您只需使用

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

希望这对你有用。