如何在目标c中解析给定的JSON数据并在表格单元格中显示

时间:2015-11-04 11:21:26

标签: ios objective-c iphone json nsarray

我有两个阵列。对于部分和部分内部A说明行B。如何访问它们以显示numberOfRowsInSection以及有关要在单元格中显示的部分的行。

我的JSON如下:

{
    "data": {
        "claims": [{
            "id": "64",
                "first_name": "Alia",
                "last_name": "Bhatt",
                "vendorimage": "http://projects.kleward.com/salesaward/beta1.5/public/profileImage/img1445325413.jpeg",
                "claimlist": [{
                "campaign_end_date": "2016-10-31",
                    "days": "362",
                    "promotion_id": "7",
                    "promotion_name": "Promotion 1 Jeans",
                    "promotion_description": " Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Description  Promotion Descripti",
                    "totalClaim": "1",
                    "totatAmount": "200.00000"
            }]
        }, {
            "id": "65",
                "first_name": "Life",
                "last_name": "Style",
                "vendorimage": "http://projects.kleward.com/salesaward/beta1.5/public/profileImage/img1445331248.jpg",
                "claimlist": [{
                "campaign_end_date": "2016-11-23",
                    "days": "385",
                    "promotion_id": "9",
                    "promotion_name": "Promotion 3 Wrist Watch",
                    "promotion_description": "Promotion 3 Wrist Watch Description",
                    "totalClaim": "1",
                    "totatAmount": "300.00000"
            }]
        }, {
            "id": "76",
                "first_name": "Snap",
                "last_name": "Deal",
                "vendorimage": "http://projects.kleward.com/salesaward/beta1.5/public/profileImage/img1445851551.png",
                "claimlist": [{
                "campaign_end_date": "2016-12-29",
                    "days": "421",
                    "promotion_id": "10",
                    "promotion_name": "Dell Promotion",
                    "promotion_description": "Dell Promotion Description",
                    "totalClaim": "1",
                    "totatAmount": "50.00000"
            }, {
                "campaign_end_date": "2016-12-30",
                    "days": "422",
                    "promotion_id": "16",
                    "promotion_name": "Mac Promotion",
                    "promotion_description": "Mac Promotion Description",
                    "totalClaim": "1",
                    "totatAmount": "150.00000"
            }]
        }]
    }
}

3 个答案:

答案 0 :(得分:0)

    NSArray *sectionArray = [[dictionary objectForKey:@"data"] objectForKey:@"claims"];
    NSInteger sectionAmount = [sectionArray count];

    for(NSDictionary *detailDict in sectionArray){
      NSInteger detailID = [[detailDict objectForKey:@"id"] integerValue];
      NSString *firstName = [detailDict objectForKey:@"first_name"];

      //And so on, do stuff here with each detail
    }

答案 1 :(得分:0)

在以下结构中解析JSon

  • 维护NSArray,其中包含每个部分的行数。
  • 维护包含每个部分的NSDictionary数组,例如Section作为Key和Array作为值。

表装饰

  • 初始化具有容量数组的数组A计数并保存每个部分的行数。要装饰行,请使用section index来获取方法中的行数"部分中的行数"
  • 从NSDictionary获取数组传递节索引以装饰节中的行。

答案 2 :(得分:0)

我的Section示例中JSON claimsclaimsrows中的对象为NSDictionary * dict = [JSON objectForKey:@"data"]; 。如果是,那么每件事都很简单:

JSON - 是收入数据

NSArray * sectionTitles = [dict allKeys];

获取要在numberOfSection中使用的部分

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return [sectionTitles count];
}

部分数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    NSString * sectionTitle = [sectionTitles objectAtIndex:section];
    NSArray * rows = [dict objectForKey:sectionTitle];
    return [rows count];
}

部分

中的行数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // init Cell

    // Get Section Title from indexPath
    NSString * sectionTitle = [sectionTitles objectAtIndex:indexPath.section];

    // Get Array of object for Section
    NSArray * objects = [dict objectForKey:sectionTitle];

    // Get Object for row
    NSDictionary * object = [objects objectAtIndex:indexPath.row];

    // Use it
    NSString * objectID = [object objectForKey:@"id"];

   // return cell;
}

indexPath的当前部分的行

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return [sectionTitles objectAtIndex:section];
}

显示章节标题

perl -i.bak -pe 'm/^4567/ and s/200/500/' filename