4

How do I convert a list of objects in JSON ? I searched the internet but all the methods that I used I go wrong. I need to turn the object coll into a string json

Collections *current = _raccolte[_currentCell];
current.label = [[alertView textFieldAtIndex:0] text];
current.datetime_last_update = [NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]];

NSMutableArray *coll = [[NSMutableArray alloc] init];
[coll addObject:current];
HttpClient *client = [[HttpClient alloc] init];
[client syncCollection:coll];
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48

3 Answers3

2

You use the NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.

Here you can check it Link

Collections *current = _raccolte[_currentCell];
current.label = [[alertView textFieldAtIndex:0] text];
current.datetime_last_update = 
[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]];

NSMutableArray *coll = [[NSMutableArray alloc] init];
[coll addObject:current.label];
[coll addObject:current.datetime_last_update];

NSError *writeError = nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:coll 
                                                   options:NSJSONWritingPrettyPrinted 
                                                     error:&writeError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData 
                                             encoding:NSUTF8StringEncoding]; 

NSLog(@"JSON Output: %@", jsonString);
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
1

There are a lot of questions about json serialization: iOS JSON serialization for NSObject-based classes

The main idea is to convert your object to NSDictionary and then convert it to jsonData and (if needed) to string.

In your case you need to create NSArray of NSDictionaries then pass it method like:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array 
                                                   options:NSJSONWritingPrettyPrinted 
                                                     error:&writeError];

Then if you need string you need to make something like:

NSString* jsonString = [[NSString alloc] initWithData:jsonData 
                                             encoding:NSUTF8StringEncoding];
Community
  • 1
  • 1
AlexZd
  • 2,152
  • 2
  • 18
  • 29
1

First, you have to be sure the objects of your collection are serializable. You can achieve this by making your Collections object compliant to the NSCoding protocol. This requires the implementation of two fundamental methods: (instancetype)initWithCoder:(NSCoder *)aDecoder and void)encodeWithCoder:(NSCoder *)aCoder.

Then you can simply use the following handler static method

- (NSString*)convertObjectToJson:(NSObject *)object {
    NSError *error = nil;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object 
                                                       options:NSJSONWritingPrettyPrinted 
                                                         error:&error];
    NSString *result = [[NSString alloc] initWithData:jsonData 
                                             encoding:NSUTF8StringEncoding];
    return result;
}

This applies for both your own Collections instances and the NSMutableArray, that is NSCoding-compliant by default.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Dree
  • 702
  • 9
  • 29