In my iPhone aplication I have a list of custom objects. I need to create a json string from them. How I can implement this with SBJSON or iPhone sdk?
NSArray* eventsForUpload = [app.dataService.coreDataHelper fetchInstancesOf:@"Event" where:@"isForUpload" is:[NSNumber numberWithBool:YES]]; SBJsonWriter *writer = [[SBJsonWriter alloc] init]; NSString *actionLinksStr = [writer stringWithObject:eventsForUpload];
and i get empty result.
I love my categories so I do this kind of thing as follows
@implementation NSArray (Extensions) - (NSString*)json { NSString* json = nil; NSError* error = nil; NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error]; json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; return (error ? nil : json); } @end
This process is really simple now, you don't have to use external libraries, Do it this way, (iOS 5 & above)
NSArray *myArray; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArray options:NSJSONWritingPrettyPrinted error:&error]; NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With