Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove backslash ('\') in objective-c

I am generating json text and while generating array I get a lot of backslashes I don't need:

[\n  
    {\n    
        \"Speed\" : 2,\n    
        \"Direction\" : 3,\n   
         \"OdometerDelta\" : 4,\n    
        \"Longitude\" : 0,\n   
         \"Latitude\" : 1,\n   
         \"TimeStamp\" : \"1996-06-17\"\n  
    },\n 
     {\n   
         \"Speed\" : 2,\n    
        \"Direction\" : 3,\n   
         \"OdometerDelta\" : 4,\n    
        \"Longitude\" : 0,\n   
         \"Latitude\" : 1,\n   
         \"TimeStamp\" : \"1996-06-17\"\n  },\n 
     {\n   
         \"Speed\" : 2,\n   
         \"Direction\" : 3,\n    
        \"OdometerDelta\" : 4,\n    
        \"Longitude\" : 0,\n    
        \"Latitude\" : 1,\n   
         \"TimeStamp\" : \"1996-06-17\"\n 
    }\n

]

This is how I get my generated json text NSString:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Here's the part where things get interesting, if I use

jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

all of the "\n" is removed (backslash is removed too), but if I use

jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];

all the backslashes are still there. I am quite new to objective-c so I can't even thought of the possible way of why this happens, I tried several ways, and none of these worked. My shot in the dark is that the problem is caused by encoding but I might be wrong.

Here is how I produce my json file:

for(int i = 0; i < 3; i++)
    {

        NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [NSNumber numberWithDouble:0.0], @"Longitude",
                                         [NSNumber numberWithDouble:1.0], @"Latitude",
                                         @"1996-06-17", @"TimeStamp",
                                         [NSNumber numberWithDouble:2.0], @"Speed",
                                         [NSNumber numberWithDouble:3.0], @"Direction",
                                         [NSNumber numberWithDouble:4.0], @"OdometerDelta",
                                         nil];


        [arr addObject:jsonDictionary];
    }


    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Thanks in advance.

like image 574
MisterMortal Avatar asked Jul 07 '14 11:07

MisterMortal


1 Answers

This line

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];

Gives you a JSON string with line feeds, spaces and indents in to format it prettily when you look at it. It does not put extra line feeds in the data. If you do this:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:0 error:&error];

the JSON string will not have any of those extra formatting characters and will appear to be all one very long line.

Your output that you have posted looks like debug output and those \n characters and \" characters are escaped by the debug description. In the actual string, they are proper line feed (character 10) and double quote characters. This is why your first replace statement works because @"\n" is a one character string with just char 10 in it. In your second statement @"\\" is a one character string with a backslash in it and there are no actual backslashes in the JSON string.

like image 148
JeremyP Avatar answered Oct 27 '22 01:10

JeremyP