In my iPhone app I want to send my data in JSON format.I have stored all the data in an dictionary.How to change dictionary data in JSON format and then send it to the serve using post method (I need to send to server only).
Asked
Active
Viewed 139 times
0
-
have you imported JSON Class files in your project? – NiravPatel Feb 11 '14 at 07:48
-
I have tried this code:-NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:AnswerData forKey:@"question"]; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:AnswerData options:NSJSONWritingPrettyPrinted error:&error];but "error" in not defined.such error i am gettig.. – user3247436 Feb 11 '14 at 07:50
-
If you are using JSON class files, then it will be pretty easy to convert your dictionary into JSON. – NiravPatel Feb 11 '14 at 07:51
-
yes i am using SBJSon class.. – user3247436 Feb 11 '14 at 07:55
-
Have you this class file NSObject+SBJSON.h and NSObject+SBJSON.m ??? – NiravPatel Feb 11 '14 at 07:56
-
check this [link](http://www.raywenderlich.com/5492/working-with-json-in-ios-5). you do not need SBJson class for serializing json data after iOS 5.0. – Pawan Rai Feb 11 '14 at 08:40
-
my dictionary data is not converting in json format and its taking all privious data tooo. – user3247436 Feb 11 '14 at 08:48
-
1. Add the code to the Q and format it. 2. What's inside the dictionary? Is there anything non JSOn-compliant? Please post it in the Q. 3. Cocoa has a JSON serializer. – Amin Negm-Awad Feb 11 '14 at 09:13
2 Answers
0
try it
NSMutableDictionary *Dict=[[NSMutableDictionary alloc] initWithObjectsAndKeys:str,@"str",str1,@"str1",Idstr,@"Idstr",nil];
NSError *error=nil;
NSData *Data=[NSJSONSerialization dataWithJSONObject:Dict options:NSJSONWritingPrettyPrinted error:&error];
NSMutableString *JsonString = [[NSMutableString alloc] initWithData:Data encoding:NSUTF8StringEncoding];
NSMutableString *urlstr=@"url"
NSURL *url=[NSURL URLWithString:urlstr];
NSMutableURLRequest *urlrequest=[NSMutableURLRequest requestWithURL:url];
NSString *urlbody=[NSString stringWithFormat:@"inputParam=%@",JsonString];
[urlrequest setHTTPBody:[Alerturlbody dataUsingEncoding:NSUTF8StringEncoding]];
[urlrequest setHTTPMethod:@"POST"];
[urlrequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
[urlrequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[urlrequest setValue:[NSString stringWithFormat:@"%d", [urlbody length]] forHTTPHeaderField:@"Content-Length"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlrequest queue:queue completionHandler:^(NSURLResponse *response, NSData *Resdata, NSError *error)
{
NSError* error;
NSDictionary *jsondict = [NSJSONSerialization
JSONObjectWithData:Resdata //1
options:kNilOptions
error:&error];
//NSLog(@"jsondict:%@",jsondict);
}
else if ([Resdata length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
else if (error != nil)
{
NSLog(@"Error happened = %@", error);
}
}];

Muralikrishna
- 1,044
- 10
- 17
0
If your using SBJSON then using the code below you can create JSON string from NSDictionary.
SBJsonWriter *jsonWriter = [SBJsonWriter new];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];
NSString *jsonString = [jsonWriter stringWithObject:dict];

Gyanendra Singh
- 1,483
- 12
- 15