Send NSMutableArray as JSON using JSON-Framework

Michi picture Michi · Jul 14, 2010 · Viewed 9.1k times · Source

I'm using JSON-Framework in my project successfully to decode JSON send from a server.

Now I need to do it the other way around and I'm facing problems as the data to be sent is a NSMutableArray fetched from CoreData.

When using

NSString* jsonString = [menuItems JSONRepresentation]

I get the message "JSON serialisation not supported for MenuItems".

Do I need to convert the NSMutableArray to some other format so the JSON-Framework can serialize it?

Thanks for any help,
Miguel

Answer

Stig Brautaset picture Stig Brautaset · Sep 21, 2010

Allow me to suggest a somewhat nicer solution:

In your MenuItems class, implement the -proxyForJson method and you should then be able to call the -JSONRepresentation method directly on the menuItems array.

@interface MenuItems(SBJson)
-(id)proxyForJson {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            self.id,@"id",
            [self.modified description],@"modified",
            nil];
}
@end

Hope this helps!