NSDictionary的;无法获得价值

时间:2012-11-14 03:00:32

标签: ios json nsdictionary

这是我用于我的字典的JSON文件:

{

    "Reports": {
        "Title of sheet": {
            "id": "1",
            "active": "0",
            "title": "Title of sheet",
            "date": "October 22, 2012",
            "description": "description",
            "ext": "DOCX",
            "fortest": "Tuesday, October 23",
            "subject": "Budget",
            "author": "xxxxxxxxxx",
            "email": "xxxxx@gmail.com"
        }
    }
}

这是我用来将其变成字典的代码(在我做一个请求之后)

self.tableDataSource = [NSMutableArray array];

 for (NSString *key in object.allKeys) // object is your root NSDictionary
    {
        NSDictionary *subDict = [object valueForKey:key];
        //create a new dictionary with two values - one for the key and one for the value.
        NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
        [newDict setValue:subDict forKey:@"value"];
        [newDict setValue:key forKey:@"key"];
        [self.tableDataSource addObject:newDict];
    }

这将生成以下输出:

(
        {
        key = Reports;
        value =         {
            "Sheet Title" =             {
                    active = 0;
                    author = "xxx xxxxxx";
                    date = "October 22, 2012";
                    description = "Description";
                    dl = "9 downloads";
                    email = "xxxxxxx@gmail.com";
                    ext = DOCX;
                    fortest= "Tuesday, October 23";
                    id = 1;
                    subject = Budget;
                    title = "Sheet title";
            };
        };
    }
)

这是我的问题...如何将“表格标题”更改为info =就像我使用报表(键)和值一样? reports数组将包含更多条目,所有条目都包含不同的“Sheet of sheet”数组。我不想具体并按名称调用数组,因为我的json总会添加数组。请帮忙,谢谢!

P.S。我尝试过使用for循环,但无济于事......


编辑:我想要这样的事情:

(
        {
        key = Reports;
        value =         {
            info =             {
                active = 0;
                author = "xxx xxxxxx";
                date = "October 22, 2012";
                description = "Description";
                dl = "9 downloads";
                email = "xxxxxxx@gmail.com";
                ext = DOCX;
                fortest = "Tuesday, October 23";
                id = 1;
                subject = Budget;
                title = "Sheet title";
            };
        };
    }
)

编辑2:

知道了!我终于通过一个颇具创造性的解决方案找到了答案我基本上将我的NSDictionary转换为NSArray并在两者之间切换,如果这是有道理的...我回家后会发布一些代码

1 个答案:

答案 0 :(得分:1)

根据我对您的问题的理解,将subDict = [NSDictionary dictionaryWithObject:[subDict valueForKey:[[subDict allKeys] objectAtIndex:0]] forKey:@"info"];添加到方法中。只需在行NSDictionary *subDict = [object valueForKey:key];

之后添加即可

例如: -

 self.tableDataSource = [NSMutableArray array];

 for (NSString *key in object.allKeys) // object is your root NSDictionary
    {
        NSDictionary *subDict = [object valueForKey:key];
        subDict = [NSDictionary dictionaryWithObject:[subDict valueForKey:[[subDict allKeys] objectAtIndex:0]] forKey:@"info"];
        //create a new dictionary with two values - one for the key and one for the value.
        NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
        [newDict setValue:subDict forKey:@"value"];
        [newDict setValue:key forKey:@"key"];
        [self.tableDataSource addObject:newDict];
    }

那应该打印,

(
        {
        key = Reports;
        value =         {
            info =             {
                active = 0;
                author = "xxx xxxxxx";
                date = "October 22, 2012";
                description = "Description";
                dl = "9 downloads";
                email = "xxxxxxx@gmail.com";
                ext = DOCX;
                fortest = "Tuesday, October 23";
                id = 1;
                subject = Budget;
                title = "Sheet title";
            };
        };
    }
)

<强>更新 根据下面的注释,您需要在另一个循环中使用subDict = [NSDictionary dictionaryWithObject:[subDict valueForKey:[[subDict allKeys] objectAtIndex:i]] forKey:@"info"];来获取所有子数组。根据您的要求修改此行。