Reputation: 3494
I need a easy to use library whit examples for converting NSObjects to JSON and back again, I found a ton of parseing examples on the net for parsing JSon but not too much on converting NSObject to JSON using SBJSON, Anybody body have a good tutorial or a sample code to convert NSObject to JSON ?
Upvotes: 2
Views: 10596
Reputation: 2395
From JSON String to Objects:
SBJsonParser *parser = [[SBJsonParser alloc] init];
// gives array as output
id objectArray = [parser objectWithString:@"[1,2,3]"];
// gives dictionary as output
id objectDictionary = [parser objectWithString:@"{\"name\":\"xyz\",\"email\":\"[email protected]\"}"];
From Objects to JSON String:
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
id *objectArray = [NSArray arrayWithObjects:@"Hello",@"World", nil];
// Pass an Array or Dictionary object.
id *jsonString = [writer stringWithObject:objectArray];
Upvotes: 2
Reputation: 15589
Using SBJson
, to convert a object to JSON string, you have to override the proxyForJson
method. Like the following,
The .h file,
@interface MyCustomObject : NSObject {
NSString *receiverFirstName;
NSString *receiverMiddleInitial;
NSString *receiverLastName;
NSString *receiverLastName2;
}
@property (nonatomic, retain) NSString *receiverFirstName;
@property (nonatomic, retain) NSString *receiverMiddleInitial;
@property (nonatomic, retain) NSString *receiverLastName;
@property (nonatomic, retain) NSString *receiverLastName2;
- (id) proxyForJson;
- (int) parseResponse :(NSDictionary *) receivedObjects;
}
In the implementation file,
- (id) proxyForJson {
return [NSDictionary dictionaryWithObjectsAndKeys:
receiverFirstName, @"ReceiverFirstName",
receiverMiddleInitial, @"ReceiverMiddleInitial",
receiverLastName, @"ReceiverLastName",
receiverLastName2, @"ReceiverLastName2",
nil ];
}
And to get the object from the JSON string you have to write a parseResponse
method like this,
- (int) parseResponse :(NSDictionary *) receivedObjects {
self.receiverFirstName = (NSString *) [receivedObjects objectForKey:@"ReceiverFirstName"];
self.receiverLastName = (NSString *) [receivedObjects objectForKey:@"ReceiverLastName"];
/* middleInitial and lastname2 are not required field. So server may return null value which
eventually JSON parser return NSNull. Which is unrecognizable by most of the UI and functions.
So, convert it to empty string. */
NSString *middleName = (NSString *) [receivedObjects objectForKey:@"ReceiverMiddleInitial"];
if ((NSNull *) middleName == [NSNull null]) {
self.receiverMiddleInitial = @"";
} else {
self.receiverMiddleInitial = middleName;
}
NSString *lastName2 = (NSString *) [receivedObjects objectForKey:@"ReceiverLastName2"];
if ((NSNull *) lastName2 == [NSNull null]) {
self.receiverLastName2 = @"";
} else {
self.receiverLastName2 = lastName2;
}
return 0;
}
Upvotes: 12
Reputation: 22633
With SBJSON, it's really simple.
NSString *myDictInJSON = [myDict JSONRepresentation];
NSString *myArrayInJSON = [myArray JSONRepresentation];
Of course, to go the other way array, do:
NSDictionary *myDict = [myDictInJSON JSONValue];
NSArray *myArray = [myArrayInJSON JSONValue];
Upvotes: 13