ios - How to remove White spaces in JSON String in Objective-c -


i converting json string nsmutabledictionary using below code,it's working fine,but if there unwanted white spaces there dictionary become null,i tested json lint, json parser, if remove manually white space json string become valid, there method remove white spaces in json string.

nsmutabledictionary  *responsedictionary;          nsdata * data = (nsdata *)responseobject;          nsstring *jsonstring =  [[nsstring alloc] initwithdata:responseobject encoding:nsutf8stringencoding];          responsedictionary = [nsjsonserialization jsonobjectwithdata:data                                                               options:nsjsonreadingmutablecontainers                                                                 error:nil];          nslog(@"the value in dic is%@",responsedictionary); 

thanks in advance

ok, if you're need yourself, here way it:

nsstring *thestring = @"    hello         long       string!   ";  nscharacterset *whitespaces = [nscharacterset whitespacecharacterset]; nspredicate *noemptystrings = [nspredicate predicatewithformat:@"self != ''"];  nsarray *parts = [thestring componentsseparatedbycharactersinset:whitespaces]; nsarray *filteredarray = [parts filteredarrayusingpredicate:noemptystrings]; thestring = [filteredarray componentsjoinedbystring:@""]; // using empty string concatenate strings 

p.s. modified version of answer (the author deserves upvote imho): https://stackoverflow.com/a/1427224/2799410


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -