0

这是我用于字典的 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 = 就像我对报告(键)和值所做的那样?报告数组将包含更多条目,其中所有条目都带有不同的“工作表标题”数组。我不想具体并按名称调用数组,因为总会有数组添加到我的 json 中。请帮忙,谢谢!

PS我试过摆弄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 并在两者之间切换,如果这有意义的话......我回家后会发布一些代码

4

1 回答 1

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"];在另一个循环中使用来获取所有子数组。根据您的要求修改此行。

于 2012-11-14T03:47:32.383 回答