Search code examples
iphoneios7

Convert NSDictionary To JSON


How to Convert NSDictionary into JSON ?

I have posted a dictionary into server using json. And in the time receiving the data from the server using GET method. the data cant be accessed using normal key calling method like

_secretanswer.text=[[NSString alloc]initWithFormat:@"%@",[customfield objectForKey:@"secanswer"]];  

the server returning a NCFString as the result.

How to convert and post a NSDictionary in the JSON format?


Solution

  • NSError * error;
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:myDictionary options:0 error:&error]; 
    
    NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]