这是我用于字典的 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 并在两者之间切换,如果这有意义的话......我回家后会发布一些代码