Dilara Albayrak
Dilara Albayrak

Reputation: 444

Compress NSDictionary in JSON format

How can i compress my NSDictionary object which is in JSON format? Do demonstrate, my current object is like that:

noncompressed version

And i want to make it be like that:

compressed version

Can you help me about that? Thanks in advance.

Upvotes: 1

Views: 307

Answers (1)

Hong Duan
Hong Duan

Reputation: 4294

It seems that you want to remove all the whitespaces, try this:

NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\s"
              withString:@""
              options:NSRegularExpressionSearch
              range:NSMakeRange(0, [jsonString length])];

Upvotes: 1

Related Questions