对于UITableview,将JSON响应提取到Propertylist中

时间:2015-12-31 06:58:05

标签: ios objective-c json uitableview

我正在尝试将JSON响应存储到属性列表中,如下面的plist结构图像。

我的回复:

{
    "response": {
        "count": "1000",
        "girls": {},
        "boys": {
            "0": {
                "name": "sam"
            },
            "1": {

                "name": "jhon"
            },
            "2": {
                "name": "keen"
            },
            "3": {

                "name": "man"
            },
            "4": {

                "name": "blue"
            }
        }
    }
}

需要实现:

Something like this

FYI:存储完所有信息后,我需要根据Girls array按钮选择获取Boys array数据和segment数据它应该在tableview上快速重新加载数据。

需要的帮助:

如何获取所有JSON数据,例如我发布的图片plist结构? 如何获取并加载到UItableview NSMutableArray

2 个答案:

答案 0 :(得分:1)

第一步:

  

将JSON字符串转换为字典

NSError *jsonError;
NSData *objectData = [strJSON dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                      options:NSJSONReadingMutableContainers 
                                        error:&jsonError];

第二步:

  

为plist制作字典

NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
[tempDict setObject:[[json objectForKey:@"response"] objectForKey:@"girls"] forKey:@"girls"];
[tempDict setObject:[[json objectForKey:@"response"] objectForKey:@"boys"] forKey:@"boys"];

最后一步:

  

将此词典写入pList文件

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"myPlistFile.plist"];
[tempDict writeToFile:plistPath atomically: YES];

编辑:如果您不想将此dict写入plist文件,请忽略上一步。

只需将tempDict用作tableView dataSource。

部分数量:[[tempDict allKeys] count]

部分中的行数:

if (indexPath.section == 0)
{
    return [[tempDict objectForKey:@"girls"] allKeys];
}
else
{
    return [[tempDict objectForKey:@"boys"] allKeys];
}

用于单元格配置,

if (indexPath.section == 0)
{
    lblTitle.text = [[[tempDict objectForKey:@"girls"] objectForKey:[NSString stringWithFormat:@"%d",indexPath.row] objectForKey:@"name"];
}
else
{
    lblTitle.text = [[[tempDict objectForKey:@"boys"] objectForKey:[NSString stringWithFormat:@"%d",indexPath.row] objectForKey:@"name"];
}

希望这会对你有帮助....

答案 1 :(得分:0)

见下面制定的例子

NSDictionary *dictionary = @{@"response": @{
                                     @"count": @"1000",
                                     @"girls": @{},
                                     @"boys": @{
                                             @"0": @{@"name": @"sam"},
                                             @"1": @{@"name": @"jhon"},
                                             @"2": @{@"name": @"keen"},
                                             @"3": @{@"name": @"man"}
                                             }
                                     }
                             };
//return boys count while loading boys.
return [[[dictionary valueForKey:@"response"] valueForKey:@"boys"] allKeys].count;

//return girls count while loading boys.
return [[[dictionary valueForKey:@"response"] valueForKey:@"girls"] allKeys].count;

//Use this in cell for indexpath for row.
NSDictionary *boys = [[dictionary valueForKey:@"response"] valueForKey:@"boys"];
NSString *nameString = [[boys valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] valueForKey:@"name"];

//To get all name of boys at once
NSArray *boysName = [boys valueForKeyPath:@"name"];