29

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.

revolutionkpi
  • 2,632
  • 10
  • 45
  • 84

6 Answers6

61

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];
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
11

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
Damo
  • 12,840
  • 3
  • 51
  • 62
6

Although the highest voted answer is valid for an array of dictionaries or other serializable objects, it's not valid for custom objects.

Here is the thing, you'll need to loop through your array and get the dictionary representation of each object and add it to a new array to be serialized.

 NSString *offersJSONString = @"";
 if(offers)
 {
     NSMutableArray *offersJSONArray = [NSMutableArray array];
     for (Offer *offer in offers)
     {
         [offersJSONArray addObject:[offer dictionaryRepresentation]];
     }

     NSData *offersJSONData = [NSJSONSerialization dataWithJSONObject:offersJSONArray options:NSJSONWritingPrettyPrinted error:&error];

     offersJSONString = [[NSString alloc] initWithData:offersJSONData encoding:NSUTF8StringEncoding] ;
 }

As for the dictionaryRepresentation method in the Offer class:

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.title forKey:@"title"];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}
Rimon
  • 178
  • 1
  • 10
2

Try like this Swift 2.3

let consArray = [1,2,3,4,5,6]
var jsonString : String = ""
do
{
    if let postData : NSData = try NSJSONSerialization.dataWithJSONObject(consArray, options: NSJSONWritingOptions.PrettyPrinted)
    {
        jsonString = NSString(data: postData, encoding: NSUTF8StringEncoding)! as String
    }
}
catch
{
    print(error)
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

Try like this,

- (NSString *)JSONRepresentation {
    SBJsonWriter *jsonWriter = [SBJsonWriter new];    
    NSString *json = [jsonWriter stringWithObject:self];
    if (!json)

    [jsonWriter release];
    return json;
}

then call this like,

NSString *jsonString = [array JSONRepresentation];

Hope it will helps you...

Venk
  • 5,949
  • 9
  • 41
  • 52
  • I suspect that the problem is the "custom objects", which the writer must somehow know how to interpret. Much easier if everything is in arrays/dictionaries. – Hot Licks Jul 23 '13 at 12:46
  • 2
    The above code is broken. Either it will fail to compile under ARC, because you call release, or it will leak memory because you only release the writer if you fail to JSONify `self`. – Stig Brautaset Aug 08 '13 at 21:11
0

I'm a bit late to this party, but you can serialise an array of custom objects by implementing the -proxyForJson method in your custom objects. (Or in a category on your custom objects.)

For an example.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39