mavericks
mavericks

Reputation: 1589

-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8943940 while parsing JSON Page to UITableView

I have been through many links and fortunately I got many similar links but nothing worked Out.

my array is appearing like this in Output, using NSLog()..

products =     (
            {
        cid = 1;
        name = "hello world";
    },
            {
        cid = 2;
        name = "It is an array";
    },
            {
        cid = 3;
        name = "error error";
    },
            {
        cid = 4;
        name = "OMG! still same";
    },
            {
        cid = 5;
        name = "any help";

  ...
  ...
   },
            {
        cid = 130;
        name = "How is the work going on.";
    },
            {
        cid = 131;
        name = "Had a nice lunch";
    }
);
success = 1

Code to parse JSON, where "news" is an Array var..

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
    news = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"Feeds...%@",news);
    [myTableView reloadData];
}

Code to display table row, here in "news" value is being displayed by NSLog(); Error comes for NSDictionary.

-(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   NSLog(@"=====%@====",news);
   NSDictionary *myDict = [news objectAtIndex:indexPath.row];
   NSArray *myArray = [myDict objectForKey:@"name"];
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
   if (cell==nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}

for (NSString *str in myArray) {
    cell.textLabel.text = str;
}
   return cell;

}

I am not able to find what i'm missing here.., Please help me out.

Thank you in advance.

Upvotes: 1

Views: 7673

Answers (4)

Midhun MP
Midhun MP

Reputation: 107121

Your JSON structure is something like this:

Dictionary-->Array-->Object (Dictionary).

So you need to do:

  1. Extract the Array from the dictionary using the key product
  2. Extract an object(Dictionary) from the array using the index
  3. Get the name from the dictionary object using the key name

Instead of this:

NSDictionary *myDict = [news objectAtIndex:indexPath.row];
NSArray *myArray = [myDict objectForKey:@"name"];

Use:

NSArray *myArr = [news objectForKey:@"products"];
cell.textLabel.text = [[myArr objectAtIndex:0] objectForKey:@"name"];

Edit

Also change your numberOfRowsInSection like:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[news objectForKey:@"products"] count];
}

Upvotes: 6

D.M Patel
D.M Patel

Reputation: 145

[news objectAtIndex:indexPath.row];

NSMutabledictionary is indexpath.row so add first news in array and that array name add in your code and you get nothing an

Upvotes: 1

ios_av
ios_av

Reputation: 324

Put NSDictionary in Array then use it in Table method

 NSMutableArray *result=[[NSMutableArray alloc] init];
  result = [googleResponse valueForKey: @"products"];


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dt = [result objectAtIndex:indexPath.row];

}  

It will Help you !!!!!

Upvotes: 0

Reinhard Männer
Reinhard Männer

Reputation: 15217

news is a NSDictionary, and a dictionary does not have a method objectAtIndex:
You have to use instead [news objectForKey:...];

Upvotes: 1

Related Questions