ios - How to set NSString value to NSMutable dictionary? -


i implementing web-service apis, data content type of json. body of request should in string format i.e in double quotes when set in dictionary, below result. me set nsstring string value in dictionary.

 nsmutabledictionary *reqparams = [nsmutabledictionary new];  [reqparams setobject:@"data.sourcestreamrequest"forkey:@"_type"];   nsmutabledictionary *reqparams1 = [nsmutabledictionary new];   [reqparams1 setobject:@"newmjpegdatasession"forkey:@"_type"];        nslog(@"%@:%@",reqparams,reqparams1); 

output

{     "_type" = "data.sourcestreamrequest"; }:{     "_type" = newmjpegdatasession; } 

could me figure out reason why ,the dictionary values shown double quotes , without double quotes. thank you

use nsjsonserialization serialize dictionary. try this:

nsdictionary *jsondictionary = [nsdictionary dictionarywithobjectsandkeys:                                 @"_type", @"data.sourcestreamrequest",                                 @"_type", @"newmjpegdatasession", nil];  nserror *error; nsdata *jsondata = [nsjsonserialization datawithjsonobject:jsondictionary options:nsjsonwritingprettyprinted error:&error]; nsstring *resultasstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; nslog(@"json:\n%@", resultasstring); 

output:

json: { "newmjpegdatasession" : "_type", "data.sourcestreamrequest" : "_type" }


Comments