Ios 国家统计局字典;难以获得价值观
这是我用于词典的JSON文件:Ios 国家统计局字典;难以获得价值观,ios,json,nsdictionary,Ios,Json,Nsdictionary,这是我用于词典的JSON文件: { "Reports": { "Title of sheet": { "id": "1", "active": "0", "title": "Title of sheet", "date": "October 22, 2012", "description": "description", "ex
{
"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"
}
}
}
这是我用来将其转换为字典的代码(在我请求之后)
这将生成以下内容的输出:
(
{
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数组将包含更多的条目,所有条目的旁边都有不同的“工作表标题”数组。我不想具体地按名称调用数组,因为总会有数组添加到我的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,并在两者之间切换,如果这有意义的话。。。回家后我会发布一些代码根据我对您问题的理解,添加
子目录=[NSDictionary Dictionary WithObject:[Subdct valueForKey:[[Subdct allKeys]objectAtIndex:0]]forKey:@“info”]编码>到方法。只需将其添加到行NSDictionary*subct=[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";
};
};
}
)
更新:
根据下面的注释,您需要使用subct=[NSDictionary dictionaryWithObject:[subct valueForKey:[[subct allKeys]objectAtIndex:i]]forKey:@“info”]在另一个循环中编码>以获取所有子阵列。根据您的要求修改此行 你不使用的原因是什么?@AJak我是为请求做的,但不是为了这个…@user1789040,所以基本上你需要的是subct=[NSDictionary dictionary dictionaryWithObject:[subct valueForKey:[[subct allKeys]objectAtIndex:0]]forKey:@“Info]代码>。只需将其添加到行NSDictionary*subct=[object valueForKey:key]之后代码>
(
{
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";
};
};
}
)