gpt4 book ai didi

objective-c - 对象-C : Convert an Array of objects into an NSDictionary for use with JSONRepresentation

转载 作者:行者123 更新时间:2023-12-04 06:16:52 25 4
gpt4 key购买 nike

我有一个包含各种信息的自定义对象的 NSMutableArray。
例如,对象可能包含:

firstname
lastname
email

我希望能够将这些对象添加到 NSDictionary,以便我可以调用 SBJSON 的“JSONRepresentation”函数,最终的 JSON 格式如下所示:
{
"Users" : [
{ "user1First" : "FirstName",
"user1Last" : "LastName",
"user1Email" : "Email" },
{ "user2First" : "FirstName",
"user2Last" : "LastName",
"user2Email" : "Email" },
{ "user3First" : "FirstName",
"user3Last" : "LastName",
"user3Email" : "Email" }
]
}

最佳答案

写个 dictionaryRepresentation对于为该特定对象返回字典的每个自定义类。然后你可以做这样的事情:

NSMutableArray *dictionaryArray = [NSMutableArray arrayWithCapacity:yourOtherArray.count];
for (id object in yourOtherArray) {
[dictionaryArray addObject:[object dictionaryRepresentation]];
}
// not sure what SBJSON's function returns so I'm using id here
id JSONRepresentation = [dictionaryArray JSONRepresentation];

关于objective-c - 对象-C : Convert an Array of objects into an NSDictionary for use with JSONRepresentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7128693/

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