简体   繁体   中英

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];

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];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM