【发布时间】:2012-11-14 03:00:32
【问题描述】:
这是我用于字典的 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 中。请帮忙,谢谢!
附:我曾尝试摆弄 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 并在两者之间切换,如果这有意义的话......我回家后会发布一些代码
【问题讨论】:
-
@AJak 我是为了这个请求,但不是为了这个......
-
@user1789040,所以基本上你需要的是
subDict = [NSDictionary dictionaryWithObject:[subDict valueForKey:[[subDict allKeys] objectAtIndex:0]] forKey:@"Info"];。只需在NSDictionary *subDict = [object valueForKey:key];行后添加即可
标签: ios json nsdictionary