0

I am converting my json string to NSMutableDictionary by using below code,it's working fine,but if there is any unwanted white spaces are there then dictionary become null,i tested it with JSON lint, JSON parser, if i remove manually that white space that JSON string become valid, there is any method to remove that 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 the dic is%@",responseDictionary);

Thanks In Advance

iphonic
  • 12,615
  • 7
  • 60
  • 107
rajesh
  • 31
  • 4
  • 4
    Sounds like it needs to be fixed in the software producing the JSON. – Droppy Sep 30 '15 at 08:03
  • please see this link : http://stackoverflow.com/questions/8075147/replace-all-nsnull-objects-in-an-nsdictionary – Anand Gautam Sep 30 '15 at 08:09
  • possible duplicate of [Collapse sequences of white space into a single character](http://stackoverflow.com/questions/758212/collapse-sequences-of-white-space-into-a-single-character) – Arthur Gevorkyan Sep 30 '15 at 08:17
  • If NSJSONSerialization doesn't parse it, then it isn't JSON. Complain to whoever produces the data. Trying to modify the data to make it parse is highly dangerous. – gnasher729 Sep 30 '15 at 08:22

2 Answers2

0

Ok, so if you're certain that you need to do it yourself, here is a way to do it:

NSString *theString = @"    Hello      this  is a   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 to concatenate the strings

P.S. This is a slightly modified version of this answer (the author deserves an upvote IMHO): https://stackoverflow.com/a/1427224/2799410

Community
  • 1
  • 1
Arthur Gevorkyan
  • 2,069
  • 1
  • 18
  • 31
0

Try this:

NSString *newString= [jsonString stringByReplacingOccurrencesOfString:@" "
                                                           withString:@""];

Now the newString will not have any white spaces.

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
Akshay Agrawal
  • 217
  • 3
  • 12
  • The PO wanted to remove not only single spaces, but all occurrences of characters of the whitespace family (e.g. newline characters, spaces, tabs, etc.). – Arthur Gevorkyan Oct 01 '15 at 08:32
  • So you can try this:
    NSString *string = @"Hello, World!"; NSCharacterSet *separator = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSArray *stringComponents = [string componentsSeparatedByCharactersInSet:separator];
    – Akshay Agrawal Oct 01 '15 at 08:47
  • That's exactly what I proposed right here: http://stackoverflow.com/a/32861064/2799410 – Arthur Gevorkyan Oct 01 '15 at 08:49
  • i tried with this ,but the result is same as previous. – rajesh Oct 01 '15 at 10:40