It looks like that post is exactly what you want, minus the formatting bit. JSONKit
is a very simple class to use. I find it more powerful than NSJSONSerialization
. But for this, I will stich NSJSONSerialization
:
// create a dictionary with the structure you want
NSDictionary *dict = @{
@"date":@"2012-12-20 12:00:00",
@"address":@"asd",
@"name":@"asdasdasdasdasd",
@"shipping_date":@"2012-12-20 12:00:00",
@"receiver_phone":@"123456789",
@"customer_phone":@"123456789",
@"total_price": @"1234",
@"items": @{
@"1": @{
@"item_id": @"1",
@"quantity": @"1",
@"type": @"1",
@"color_id": @"0"
}
}
};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:nil error:&error];
if ( error ) {
// handle accordingly
}
else if ( jsonData != nil ) {
NSString *jsonRequest = [[NSString alloc] initWithData:jsonData encoding:NSUTF*Encoding];
// build request like example
}
Now you can use the rest of the example.
UPDATE:
I find JSONKit
much more powerful than NSJSONSerialization
and requires less steps, but NSJSONSerialization
should produce the same results, and does not require worrying about mixing ARC and non-ARC code.
UPDATE 2:
I would recommend giving JSONKit
a serious look, it is less picky (i.e. values can be an NSNumber
instead of NSString
). It does require setting the -fno-objc-arc
flag in the build phases for JSONKit.m
.