获得" \ n"在websocket / SignalR响应?

时间:2017-03-02 16:32:52

标签: ios objective-c websocket signalr

我正在" \ n"在websocket / SignalR响应中,我似乎无法弄清楚原因。我已经尝试更改响应对象的数据类型,将其解析为好像它是JSON,但仍然无法摆脱" \ n"。

如何删除此&#34; \ n&#34;并像处理任何其他JSON对象/响应那样处理它?<​​/ p>

参考代码:

-(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 个答案:

答案 0 :(得分:1)

简单。

您可以使用:

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

但这个伎俩是&#34;脏&#34;因为问题显然来自你的网络服务,我想。

修改

你在哪里取代事件?

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

[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);
}

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