I want to convert NSDictionary's and NSArrays to json format. Can JSON Kit do that and how?
// data->string
NSMutableDictionary *nameElements = [NSMutableDictionary dictionary];
[nameElements setObject:@"abcd" forKey:@"username"];
[nameElements setObject:@"1234" forKey:@"password"];
NSString* jsonString = [nameElements JSONString];
// string->data
NSDictionary *nameElements_ = [jsonString objectFromJSONString];
for(NSString *key in [nameElements_ allKeys]) {
NSString* body = [nameElements_ objectForKey:key];
NSLog(@"%@", body);
}
This is an example of sending dictionary and array to server.which worked for me 1000000% .
SBJSON *jparser = [[SBJSON new] autorelease];
NSString *ArrayjsonItems = [jparser stringWithObject:self.UrMergedArray];
NSString *DicjsonItems = [jparser stringWithObject:self.UrMergedDic];
NSLog(@"array Items :%@",self.UrMergedArray);
NSLog(@"dic Items :%@",self.UrMergedDic);
NSString *postString =[NSString stringWithFormat:@"Arrayitems=%@&Dicitems=%@",ArrayjsonItems,DicjsonItems];
NSLog(@"it is going to post : %@ \n\n",postString);
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:snapURL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection=[[NSURLConnection alloc]
initWithRequest:request
delegate:self];
if (connection) {
self.receivedData = [[NSMutableData data] retain];
}
[connection release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With