【问题标题】:Retrieve Data and save JSON records in an array检索数据并将 JSON 记录保存在数组中
【发布时间】:2015-01-19 03:17:09
【问题描述】:

我需要在tableview 中显示address。如何破解JSON 并将地址保存在NSArray 中。

JSON 是:

{
    "Rank": 54,
    "data": [
        {
            "number": 1,
            "address": "x mandt "
        },
        {
            "number": 2,
            "address": "x mandt2 "
        }
    ]
}

代码是:

NSDictionary *dic = (NSDictionary *) responseObject;
NSDictionary * dat  = [dic objectForKey:@"data"];
NSArray *add =[dat objectForKey:@"address"];

上面的代码并没有检索和保存add数组中的所有地址。我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c json afnetworking


    【解决方案1】:

    假设这是你的序列化数据

    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
    
     // here I start your work 
    
      NSArray *infoDict=[jsonArray objectForKey:@"data"];
    
      for (NSDictionary *tmp in infoDict)
         {
      NSMutableDictionary *temparr=[[NSMutableDictionary alloc]init];
             [temparr setValue:[tmp objectForKey:@"number"] forKey:@"number"];
             [temparr setValue:[tmp objectForKey:@"address"] forKey:@"address"];
             [_tdataSource addObject:temparr];
            }
      [yourtableviewNAme reloadData];
    

    这里我添加了 Tableview DataSourcedelegate 方法

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
    {
    return [self.tdataSource count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     static NSString *CellIdentifier = @"resultCell";
    yourtableviewCellName *cell = [self.yourtableName dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[yourtableviewCellName alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    
        }
    cell.textLabel.text=[[self.tdataSource objectAtIndex:indexPath.row]objectForKey:@"address"];
    
        return cell;
    }
    

    【讨论】:

    • 如果您需要任何帮助,我希望与您合作
    【解决方案2】:

    我认为你最好只使用文字语法来检索它。您检索的方式很好。您可能只是添加了一些自省:

    NSDictionary *responseDict = (NSDictionary *) responseObject;
    if (responseDict.count) {
       NSArray *dataArray  = responseDict[@"data"];
       if (dataArray.count) {
          // do whatever you want
       }
    }
    

    你在检索关键字data时出错了,之后你会得到一个数组,而不是NSDictionary

    【讨论】:

      【解决方案3】:

      应该是:

      NSArray *add =[dic objectForKey:@"data"];
      

      如果你想要地址(我正在考虑第 0 个索引中的地址),那么这样做:

      NSString *str = [[add objectAtIndex: 0] objectForKey:@"address"];
      

      编辑:

      声明一个类变量,如:

      @interface YourClassName (){
          NSMutableArray *dataSource;
      }
      

      像这样填充数据源:

      dataSource =[dic objectForKey:@"data"];
      

      然后在您的 cellForRowAtIndexPath 方法中执行以下操作:

                  cell.textLabel.text = [[dataSource objectAtIndex:indexPath.row] objectForKey:@"address"];
      

      我正在考虑您的表格视图中有单个部分。希望这会有所帮助.. :)

      【讨论】:

      • 我希望它保存在一个数组中,这样我就可以将它填充到一个表格视图中
      • numberOfRowsInSection 中,计数总是返回 0。return [_tdataSource count].我该如何解决这个问题?
      • @Illep > 你能分享你在哪里填充 _tdataSource 吗??
      • 你确定你的 JSON 数据在你加载你的 tableview 时已经被返回和解析了吗?也许你对numberOfRowsInSection: 的实现应该更像return [_tdataSource count]? : 1;
      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多