1

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.

4

4 回答 4

6

你的 JSON 结构是这样的:

字典-->数组-->对象(字典)。

所以你需要这样做:

  1. 使用键从字典中提取数组product
  2. 使用index
  3. 使用键从字典对象中获取名称name

而不是这个:

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

利用:

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

编辑

也改变你的numberOfRowsInSection喜欢:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[news objectForKey:@"products"] count];
}
于 2013-05-13T12:18:05.770 回答
1

newsNSDictionary, 字典没有方法objectAtIndex:
你必须使用[news objectForKey:...];

于 2013-05-13T12:12:17.783 回答
1
[news objectAtIndex:indexPath.row];

NSMutabledictionary 是 indexpath.row 所以在数组中添加第一个新闻,然后在你的代码中添加数组名,你什么也得不到

于 2013-05-18T09:31:24.300 回答
0

将 NSDictionary 放在 Array 中,然后在 Table 方法中使用它

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


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

}  

它会帮助你!!!!

于 2013-05-13T12:19:19.680 回答