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
Post a Comment