删除objective-c中的反斜杠('\') [英] Remove backslash ('\') in objective-c

查看:72
本文介绍了删除objective-c中的反斜杠('\')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成 json 文本,在生成数组时,我得到了很多我不需要的反斜杠:

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

]

这就是我获取生成的 json 文本 NSString 的方式:

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:@""];

所有的\n"都被删除了(反斜杠也被删除了),但是如果我使用

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

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

所有的反斜杠仍然存在.我对objective-c很陌生,所以我什至想不出发生这种情况的可能方式,我尝试了几种方法,但这些方法都没有奏效.我在黑暗中的镜头是问题是由编码引起的,但我可能是错的.

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.

这是我生成 json 文件的方法:

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];

提前致谢.

推荐答案

此行

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

为您提供一个带有换行、空格和缩进的 JSON 字符串,以便在您查看时对其进行漂亮的格式化.它不会在数据中放入额外的换行符.如果你这样做:

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];

JSON 字符串不会包含任何这些额外的格式字符,并且看起来都是很长的一行.

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

您发布的输出看起来像调试输出,那些 \n 字符和 \" 字符被调试描述转义.在实际的字符串中,它们是正确的换行符(字符 10)和双引号字符.这就是为什么您的第一个替换语句起作用的原因,因为 @"\n" 是一个只有字符 10 的字符串.在您的第二个语句中 @"\\" 是一个带有反斜杠的单字符字符串,JSON 字符串中没有实际的反斜杠.

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.

这篇关于删除objective-c中的反斜杠('\')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆