30

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.

5
  • Define "custom objects". Commented Jul 23, 2013 at 12:17
  • what is the problem do you have in this given code?? Commented Jul 23, 2013 at 12:19
  • Does your array contains data?? Commented Jul 23, 2013 at 12:22
  • See this question similar to yours : stackoverflow.com/questions/9139454/… Commented Jul 23, 2013 at 12:23
  • @revolutionkpi I know it's an old question, but it might help others if you accepted one of the answers as a solution to mark the question as solved Commented Aug 20, 2019 at 8:43

6 Answers 6

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];
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Better with a little simple error control: return (error ? nil : json);
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];
}

Comments

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

Comments

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

2 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.