2

我有一个包含各种信息的自定义对象的 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" }
               ]
}
4

1 回答 1

3

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];
于 2011-08-20T00:34:55.563 回答