2017-03-02 52 views
2

我收到了一个websocket/SignalR响应中的“\ n”,我似乎无法弄清楚为什么。我试图改变我的响应对象的数据类型,解析它,就好像它是JSON,但仍然无法摆脱“\ n”。在websocket/SignalR响应中获取“ n”?

我该如何删除这个“\ n”并像其他任何JSON对象/响应一样处理?

代码以供参考:

-(void)SignalR{ 

    WebServices *services = [[WebServices alloc] init]; 

    SRHubConnection *hubConnection = [SRHubConnection connectionWithURLString:@"xxx"]; 

    SRHubProxy *proxy = [hubConnection createHubProxy:@"xxx"]; 

    [services callGetSRAlertGroupNames:^(NSMutableArray *alertGroupNameArray){ 
     NSLog(@"SR ALERT GROUP NAMES: %@", alertGroupNameArray); 

     [services callGetSRNotificationGroupNames:^(NSMutableArray *notificationGroupNameArray) { 
      NSLog(@"SR NOTIFICATION GROUP NAMES: %@", notificationGroupNameArray); 

      NSArray *combinedArray=[alertGroupNameArray arrayByAddingObjectsFromArray:notificationGroupNameArray]; 

      // Register for connection lifecycle events 
      [hubConnection setStarted:^{ 

       NSLog(@"Connection Started"); 

       for (NSString *groupName in combinedArray){ 
        [proxy invoke:@"Subscribe" withArgs:@[groupName] completionHandler:nil]; 
       } 

      }]; 
      [hubConnection setReceived:^(NSString *data) { 

       NSLog(@"CONNECTION RECIEVED - %@",data); 

      }]; 
      [hubConnection setConnectionSlow:^{ 
       NSLog(@"Connection Slow"); 
      }]; 
      [hubConnection setReconnecting:^{ 
       NSLog(@"Connection Reconnecting"); 
      }]; 
      [hubConnection setReconnected:^{ 
       NSLog(@"Connection Reconnected"); 
      }]; 
      [hubConnection setClosed:^{ 
       NSLog(@"Connection Closed"); 
      }]; 
      [hubConnection setError:^(NSError *error) { 
       NSLog(@"Connection Error %@",error); 
      }]; 

      [hubConnection start]; 

     }]; 
    }]; 
} 

已注销响应样本:

CONNECTION RECIEVED - { 
    A =  (
     "{ 
\n \"NotificationType\": 1, 
\n \"TelemetryDetails\": { 
\n \"serialNumber\": \"xxx\", 
\n \"name\": \"sf-top\", 
\n \"statusId\": 2, 
\n \"buildVersion\": \"xxx\", 
\n \"securityModeId\": 2, 
\n \"IP\": \"xxx\", 
\n \"priority\": 1, 
\n \"bandwidthUpload\": 0.00, 
\n \"bandwidthDownload\": 0.00, 
\n \"bandwidthInternal\": null, 
\n \"totalBandwidthUpload\": 3107397.00, 
\n \"totalBandwidthDownload\": 8078656.00, 
\n \"totalBandwidthInternal\": null, 
\n \"usage\": \"8078656/3107397\", 
\n \"lastUpdateTime\": \"2017-03-02T16:27:57.1736937Z\", 
\n \"buildVersionUpdatingInProgress\": false, 
\n \"transportType\": 2, 
+1

从我的经验来看,这不是信号R的正常响应。有一个机会就是您的后端如何设置向您发送信息 - 您是否检查过要排除? – Kiley

回答

1

简单。

您可以使用:

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

但因为这个问题显然是从您的网络服务来这一招是“脏”,我猜。

编辑:

你在哪里更换出现?

你能告诉我这个日志是什么吗?

[hubConnection setReceived:^(NSString *data) { 

     NSLog(@"CONNECTION RECIEVED - %@",data); 
     NSString *newString = [data stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
     NSLog(@"NEW STRING - %@", newString); 
    }]; 

编辑2:

好了,你可以尝试添加完成处理器超出这个变量。

__block NSString *newString; 

或者你也可以试试这个

[hubConnection setReceived:^(NSString *data) { 

      NSLog(@"CONNECTION RECIEVED - %@",data); 

      [self replaceOccurrences: data]; 

}]; 

- (void)replaceOccurrences:(NSString *)data { 
    NSString *newString = [data stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
    NSLog("New String = %@", newString); 
} 

但你真的应该检查你的后端,这种反应是不以适当的格式。

+0

不幸的是它打破了这一行,表明它是一本字典。如果我让我的数据对象成为一个NSDictionary,如果我记录该字典,我仍然会得到“\ n”。问题可能与数据的发送方式有关。 – arcade16

+0

该语句导致以下错误: - [__ NSDictionaryI stringByReplacingOccurrencesOfString:withString:]:无法识别的选择器发送到实例0x6180002665c0 – arcade16