0

In my app, I am making a share function that sends another user an email with a URL to open my app and pass parameters. I googled around for a bit and came up with this.

I know its a terrible example, but that is all I could find. Since I need to pass a NSNumber, a NSDate, and an NSString, I figured this would be the most convenient way to do so. Right now, I cannot make sense of that code. I went ahead and got the SBJson library, which is what he appears to be using. Can someone help me out on this?

Keaton
  • 127
  • 14

1 Answers1

1

You don't have to use a library for JSON Serialization in iOS. From iOS 5.0 onwards there is a class NSJSONSerialization is available for doing this. https://developer.apple.com/library/ios/documentation/foundation/reference/nsjsonserialization_class/Reference/Reference.html

Consider this code for converting list of contacts to json string

+(NSString*) jsonStringRepresentationOfSelectedReceiptients:(NSArray*) contactsList {
  NSMutableArray* contactsArray = [[NSMutableArray alloc]init];
  for (ContactObject *contactObj in contactsList) {
        NSDictionary* ltPair = @{@"phone_no": [NSNumber numberWithLongLong:contactObj.phoneNo, @"email_id": contactObj.emailId, @"display_name": contactObj.displayName], @"creation_date": [NSDate dateWithTimeIntervalSince1970:contactObj.timeStamp]]};
        [contactsArray addObject:ltPair];
  }
  NSData *jsonData=[NSJSONSerialization dataWithJSONObject:contactsArray options:NSJSONWritingPrettyPrinted error:nil];
  NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  NSLog(@"json string: %@", recptJson);
  return jsonString;
}

If you have proper NSDate instead of time stamp, u can directly use it. Hope this helps.