JSON Object to NSArray

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?

Upvotes: 3

Views: 2174

Answers (3)

Ismael
Ismael

Reputation: 3937

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

Upvotes: 2

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

Upvotes: 0

Nick Bull
Nick Bull

Reputation: 4286

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.

Upvotes: 0

Related Questions