【问题标题】:How to get and load nested JSON values into Tableview Objective c?如何获取嵌套 JSON 值并将其加载到 Tableview Objective c 中?
【发布时间】:2015-07-31 11:27:08
【问题描述】:

下面我发布了我的嵌套 JSON 响应。我想将所有键和键值加载到一个表视图中,如下面的 UI。请帮帮我!

我的 JSON

response : {

     ANI =  { 
             name = "anisharmu";
             age  = "10";
     };

     ROC =  { 
             name = "rockins";
             age  = "20";   
     };
}

我的 UI 表格视图

|------------------------|
  ANI anisharmu - 10
|------------------------|

我的代码

 NSError *error;
 jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
 // get keys from response dictionary
 NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[jsonDictionary[@"response"] allKeys]];

 // sort as asending order
 NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
 key =  (NSMutableArray *)[key sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

 // access inner data from dictonary
 for (NSString * obj in key) {

            NSLog(@"%@",jsonDictionary[@"response"][obj][@"name"]);

 }

【问题讨论】:

标签: ios objective-c json uitableview


【解决方案1】:

首先解析响应数据,如下所示:

NSData *responseData;
        NSError *error;
        NSDictionary *jSONDictonary = [NSJSONSerialization dataWithJSONObject:responseData options:kNilOptions error:&error];

        NSArray *allKeys = [jSONDictonary allKeys];

然后在你的 cellforRowAtIndexpath 方法中编写以下代码:

cell.mainName.text  = [allKeys objectAtIndex:indexpath.row];
    cell.namelabel.text = [jSONDictonary valueForKeyPath:[NSString stringWithFormat:@"%@.name",[allKeys objectAtIndex:indexpath.row]]];
    cell.ageLabel.text  = [jSONDictonary valueForKeyPath:[NSString stringWithFormat:@"%@.age",[allKeys objectAtIndex:indexpath.row]]];

【讨论】:

  • 它的。导致崩溃 -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]' @Vikram
  • 它没有得到名称值@vikram
  • 您能在此处为我添加您的代码吗?让我看看
  • 你可以看到我上面的代码。但它不符合条件。
  • 你明白了吗?
【解决方案2】:

这里的响应是一本字典。

如果您使用的是AFNetworking,则只需以NSDictionary 的形式访问响应。您可以通过访问它们的键来获取值。

NSDictionary *aniDict =[ responseDict objectForKey:@"ANI"]; 将返回 ANI 键下的对象。

也可以访问嵌套字典的新字典。

NSString *name = [aniDict valueForKey:@"name"];
int age =  [[aniDict objectForKey:@"age"] integerValue];

数字和布尔值是字典中的 NSSNumber 实例,需要解包。

【讨论】:

  • 请提供示例代码。这很紧急! @Vendetta 土耳其
  • 但是 NSDictionary 给出了 UNorder 输出
  • NSDictionary 主要是一个地图,你可以不分先后顺序访问对象!!!在您的问题中变得更有条理。提供您如何获得回复。你向人们索要代码示例,但你甚至没有提供你的请求是如何被获取的。
【解决方案3】:

联系你的脚本制作者并打电话给他制作这样的脚本

     response : [

                 {  
                     id=ANI;
                     name = "anisharmu";
                     age  = "10";

                  }

              {      id =ROC;  
                     name = "rockins";
                     age  = "20";
              }
                  ];
        }
    NSMutableArray *ArrData=[responseDict objecforKeys:@"response"];//all data in array now just put it in your tableview
    int i=[ArrData count];//all keys



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return [ArrData count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %ld",(long)indexPath.row];



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    cell = nil;

    if (cell == nil)

    {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UILabel *lbl_id=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
        lbl_id.text = [[ArrData objectAtIndex:indexPath.row] objectForKey:@"id"];
        [cell.contentView addSubview:lbl_id];
        lbl_id.layer.borderColor=[UIColor blueColor].CGColor;
        lbl_id.layer.borderWidth=4.0;
        [lbl_id release];

        UILabel *lblName=[[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 40)];
        lblName.text = [[ArrData objectAtIndex:indexPath.row] objectForKey:@"name"];
        [cell.contentView addSubview:lblName];
        lblName.layer.borderColor=[UIColor blueColor].CGColor;
        lblName.layer.borderWidth=4.0;

        [lblName release];

        UILabel *lblage=[[UILabel alloc] initWithFrame:CGRectMake(10, 70, 100, 40)];
        lblage.text=[[ArrData objectAtIndex:indexPath.row] objectForKey:@"age"];
        [cell.contentView addSubview:lblage];
        lblage.layer.borderColor=[UIColor blueColor].CGColor;
        [lblage release];



    }



    return cell;

}

【讨论】:

  • 好的,谢谢@Kandhal
  • 现在这样做你的 JSON 输出有问题,它永远不会显示你想要得到的输出.....说你的脚本制作者这样做
  • 原因:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例 0x7b6385a0' 崩溃
  • 你的 JSON 结构是这样的:Dictionary-->Array-->Object (Dictionary)。所以你需要做: 使用键响应从字典中提取数组 使用索引从数组中提取一个对象(字典) 使用键名从字典对象中获取名称它的简单 yar
  • 你修改了我的 JSON 响应的第一件事。请检查上面我发布的内容。
猜你喜欢
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多