2

I have an NSMutableArray of custom objects with various information them. For instance the object might contain:

firstname
lastname
email

I'd like to be able to add these objects to an NSDictionary, so that I can call SBJSON's 'JSONRepresentation' Function, and the final JSON format would look like:

{
    "Users" : [
               { "user1First" : "FirstName",
                 "user1Last"  : "LastName",
                 "user1Email" : "Email" },
               { "user2First" : "FirstName",
                 "user2Last"  : "LastName",
                 "user2Email" : "Email" },
               { "user3First" : "FirstName",
                 "user3Last"  : "LastName",
                 "user3Email" : "Email" }
               ]
}
Adam Johnson
  • 2,198
  • 1
  • 17
  • 23
  • I think you should clarify this question a bit more. Are you trying to generate a number of users with the same values as "FirstName" etc? I don't understand well enough to help. – Nicolas Renold Aug 20 '11 at 00:32
  • "FirstName", "LastName" and "Email" are the keys for the JSON Values. – Adam Johnson Aug 20 '11 at 01:42

1 Answers1

3

Write a dictionaryRepresentation for each of your custom classes that returns a dictionary for that particular object. Then you can do something like this:

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];
adamrothman
  • 956
  • 9
  • 9