2017-03-02 66 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