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.
Your JSON structure is something like this:
Dictionary-->Array-->Object (Dictionary).
So you need to do:
- Extract the Array from the dictionary using the key
product
- Extract an object(Dictionary) from the array using the
index
- 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];
}
news
is a NSDictionary
, and a dictionary does not have a method objectAtIndex:
You have to use instead [news objectForKey:...];
[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
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 !!!!!
来源:https://stackoverflow.com/questions/16521692/nscfdictionary-objectatindex-unrecognized-selector-sent-to-instance-0x894