gpt4 book ai didi

ios - NS字典;获取值时遇到困难

转载 作者:行者123 更新时间:2023-11-29 04:18:12 25 4
gpt4 key购买 nike

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

最佳答案

根据我对您问题的理解,将 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"]; 来获取所有子数组。根据您的要求修改此行。

关于ios - NS字典;获取值时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com