在NSMutableString中替换“\ r \ n”,从服务器接收数据

时间:2012-02-15 19:45:26

标签: iphone json utf-8 replace nsmutablestring

我通过执行以下代码从服务器获取数据:

- (void) sendGeneral:(NSString *) general{

    self.responseData = [NSMutableData data];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:CBURL]];

    [request setHTTPMethod:@"POST"];
    int i;
    i = [general length];
    NSLog(@"Length is %i", i);

    [request setHTTPBody:[general dataUsingEncoding:NSUTF8StringEncoding]];

      [[NSURLConnection alloc] initWithRequest:request delegate:self];

}

我在NSLog之后获得的数据到控制台包含" \ r \ n"正如您从以下数据中看到的那样(只有部分数据要演示)

"numberPhoneFrom":"","FormName":"הרשמה\r\n"},{"CallID":314358,"StartDate":"27/07/2011 19:32","TotalTime":51.0,"numberPhoneFrom":"","FormName":"פרטים כלליים\r\n"},{"CallID":424378,"StartDate":"26/09/2011 18:43","TotalTime":63.6,"numberPhoneFrom":"","FormName":"הרשמה\r\n"},{"CallID":311229,"StartDate":"26/07/2011 13:28","TotalTime":18.6,"numberPhoneFrom":"","FormName":"נסיון\r\n"}

我试图清理" \ r \ n"使用所有类型的命令,但我无法执行此代码获取协议中的数据

- (void) GetData: (NSMutableString *)protocol{
     [protocol replaceOccurrencesOfString:@"\n"  withString:@"" options:0 range:NSMakeRange(0, [protocol length])]; 
      NSLog(@"server data Function%@", protocol);
} 

在NSMutableString中收听服务器中的数据

/*****************************************************************/
/* connection finish loading and continue talk                   */
/*****************************************************************/

    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        [connection release];

       NSMutableString* responseString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];


      //  NSLog(@"the Server Response was %@", responseString);
         [[self delegate] GetData:responseString];


    }

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

可能试试这个:

[protocol replaceOccurrencesOfString:@"\\r\\n"  withString:@"" options:0 range:NSMakeRange(0, [protocol length])]; 

答案 1 :(得分:0)

**在服务器上解决了这个问题,我认为这是最好的事情'服务器更强大,C#在字符串handeling中非常好

谢谢。**