将JSON响应提取到UITableview的属性列表中 [英] Fetch JSON Response Into Propertylist For UITableview

查看:126
本文介绍了将JSON响应提取到UITableview的属性列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON响应存储到属性列表中,如下所示:plist的图像结构化.

I am trying to store JSON response Into propertylist like below Image of plist structured.

我的回应:

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

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

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

                "name": "blue"
            }
        }
    }
}

需要实现:

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

FYI: After stored all the Information's I need to get Girls array data and Boys array data based on segment button selection It should reload the data quickly on tableview.

需要帮助:

如何像我发布的图像plist结构那样获取所有JSON数据? 如何获取并将其加载到UItableview NSMutableArray?

How to fetch all the JSON data like my posted Image plist structure? How to get and load It Into UItableview NSMutableArray?

推荐答案

第一步:

将JSON字符串转换为字典

Convert JSON string to dictionary

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

第二步:

为plist制作字典

Make a dictionary for 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文件

Write this dictionary to pList file

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

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

如果您不想将此字典写入plist文件,请忽略最后一步.

EDIT : If you dont want to write this dict to plist file, ignore last step.

只需将您的tempDict用作tableView数据源.

Just use your tempDict as 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"];
}

希望这对您有帮助....

Hope this will help you....

这篇关于将JSON响应提取到UITableview的属性列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆