3

I have to following JSON response from a server:

   {
  "theSet": [

  ],
  "Identifikation": 1,
  "Name": "Casper",
  "Adress": "Lovis 23",
  "Location": "At home",
  "Information": "The first, the second and the third",
  "Thumbnail": "none",
 }

I am retrieving the data like so:

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

    NSLog(@"connectionDidFinishLoading");

    NSLog(@"Succeeded! Received %d bytes of data",[data length]);

    NSError *myError = nil;

     NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];

      news = [NSArray arrayWithObjects:[res allKeys], [res allValues], nil];

      NSLog(@"%@", news);

    //[mainTableView reloadData];
}

Then I want to insert all the JSON data into an array, so I can display the data in my tableview.

My tableview code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Adress"];

    return cell;
}

But my app crashes with the error:

-[__NSArrayI objectForKey:]: unrecognized selector sent to instance. 

How can I insert the JSON object into an NSArray, so I can display it in my tableview?

3 Answers 3

2

EDITED:

I reviewed your code again and what I previously answered was wrong.

When generating your news, you are putting 2 NSArray objects in it. The first containing all keys, and the second containing all values in your JSON.

In order to display the names of each object in your JSON, you should be simply doing

news = [res allKeys];
jsonResult = res;

// store your json if you want to use the values!

Note they will be unordered. On your cell, you can do:

NSString *key = [news objectAtIndex:indexPath.row];
cell.textLabel.text = key;

id object = [jsonResult valueForKey:key];
cell.detailTextLabel.text = // do something depending on your json type which can have different values
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your very quick answer and your useful information. I am just trying to display the name, from the JSON object in my tableview. Can you please guide me in the right direction, what I have to write instead of [news objectAtIndex:indexPath.row] and objectForKey, to get it to work?
Check my edit, I got confused when reading your code and my previous answer was incorrect.
You are assuming that there is only ever one record being returned and so there will only ever be one set of values per key. You are also just displaying all of the values on the list. So the table will just list all of the values from one record
That you very much for your answers and help, I really appreciate it! Both your answers is very good and helpful, but I can only "accept" one answer :O
0

The error you have

[__NSArrayI objectForKey:]: unrecognized selector sent to instance.

Tells you everything you need to know.

This says NSArray does not understand objectForKey. If you read the documentation provided by Apple, you will see this.

Your code

cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];

Is expecting the news NSArray to return an object that responds to objectForKey - most likely an NSDictionary. The code you have for extracting your JSon data

NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
news = [NSArray arrayWithObjects:[res allKeys], [res allValues], nil];

Is just taking all of the dictionaries and extracting the keys into the array.

You need to look at these lines of your code - this is where you are going wrong.

Look at the NSJSONSerialization reference and the releated sample code that it links to.

1 Comment

I got caught up with something at work and then forgot to post something. Your answer was quite vague and unclear. Forwarding someone to the documentation without even hinting him where his problem was isn't the best practice, because not everyone is an experienced objc programmer and not all can start digesting every bit of documentation to understand a simple error like this.
0

I found a simple solution myself, that fits my project better:

I just did the following:

    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&myError];

NSArray news = [NSArray arrayWithObject:res];

and with that I am able to use the following code to display the JSON contents in my tableview.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }


    cell.textLabel.text = [[news objectAtIndex:indexPath.row] valueForKey:@"name"];

    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Location"];

    return cell;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.