revolutionkpi
revolutionkpi

Reputation: 2682

create a json string from NSArray

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.

Upvotes: 30

Views: 38939

Answers (6)

Mohammad Kamran Usmani
Mohammad Kamran Usmani

Reputation: 878

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)
}

Upvotes: 2

Stig Brautaset
Stig Brautaset

Reputation: 2632

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.

Upvotes: 0

Rimon
Rimon

Reputation: 188

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];
}

Upvotes: 6

Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

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];

Upvotes: 61

Damo
Damo

Reputation: 12890

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

Upvotes: 11

Venk
Venk

Reputation: 5955

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...

Upvotes: 0

Related Questions