Damir
Damir

Reputation: 56199

How to convert NSDictionary to NSString which contains json of NSDictionary?

How to convert NSDictionary to NSString which contains JSON of NSDictionary ? I have tried like but without success

//parameters is NSDictionary
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                       options:0
                                                         error:&error];

Upvotes: 2

Views: 14540

Answers (2)

nsgulliver
nsgulliver

Reputation: 12671

if jsonData is NSDictionary

NSString *str=[NSString stringWithFormat:@"json data is %@", jsonData];

OR if jsonData is NSData

NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];

Upvotes: 5

Rob
Rob

Reputation: 437552

If you just want to inspect it, you can create a NSString:

NSString *string = [[NSString alloc] initWithData:jsonData
                                         encoding:NSUTF8StringEncoding];

But if you're writing it to a file or sending it to a server, you can just use your NSData. The above construct is useful for examining the value for debugging purposes.

Upvotes: 4

Related Questions