views:

71

answers:

2

Hi all,

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

A: 

I finally solved it, but I'm not sure if it's the best/most elegant way to do it.

NSMutableArray* items = [[NSMutableArray alloc] init];
for (MenuItems* item in menuItems) {
    [items addObject:[NSArray arrayWithObjects:item.id,[item.modified description],nil]];
}
NSString *post = [NSString stringWithFormat:@"currentData=%@",
                  [items JSONRepresentation]];

Explanation:
I first thought that the problem was the NSMutableArray, but then realized that it was the contents of it. So I just get the information I need out of it and saved it as NSArray which JSON-Framework does accept :-)

Michi
A: 

Hi Michi,

Allow me to suggest a somewhat nicer solution:

In your MenuItems class, implement the -proxyForJson method to return [NSArray arrayWithObjects:self.id,[self.modified description],nil] and you should be able to call the -JSONRepresentation method directly on the menuItems array.

Hope this helps!

Stig Brautaset
Hi Stig,I will try that... thanks :-)
Michi
Did you get it to work?
Stig Brautaset
Hi Stig, I did not yet find the time to test it... will tell you as soon as I get around to try it.
Michi