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

时间:2022-02-23 21:04:25

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

嗨我是ios的初学者,在我的项目中,我试图获得低于Json数组数据,但它显示异常(如 - [__ NSArrayM objectForKey:]:无法识别的选择器发送到实例0x7be53f80)我做错了什么,请帮帮我,在这里数据已成功加载到我的主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);
    }

3 个解决方案

#1


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..

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

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

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

#2


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.

通过查看您的JSON FROMAT.It很清楚是Array.So请将您的响应json放入Array,其他代码是正确的。

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 . . . .

它会工作正常。 。 。 。

#3


1  

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

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

For example like this:

例如这样:

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

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

数据是你的JSON对象,你可以简单地使用

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

Hope this will work for you.

希望这对你有用。

#1


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..

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

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

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

#2


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.

通过查看您的JSON FROMAT.It很清楚是Array.So请将您的响应json放入Array,其他代码是正确的。

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 . . . .

它会工作正常。 。 。 。

#3


1  

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

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

For example like this:

例如这样:

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

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

数据是你的JSON对象,你可以简单地使用

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

Hope this will work for you.

希望这对你有用。