问题
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.
回答1:
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.
回答2:
sonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
will not remove \n it will remove newline characters, which raises the question how are you getting the output you display, is that what really is in the string or is that just information generated on output to show what is a newline character and a quote instead of the end of the string.
Also to remove \" from your string you should really be doing this
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
otherwise you run the risk of removing \ characters that are suppose to be there.
来源:https://stackoverflow.com/questions/24609224/remove-backslash-in-objective-c