2017-03-09 57 views
1

我正在使用SignalR-ObjC的應用程序。我建立了與我們的服務器的連接,並且正在接收正確的數據,但響應對象正在拋棄我。我試圖解析對JSON和字典的響應,但它看起來像SignalR-ObjC將響應封裝在額外的數據中,這阻止了我將它解析爲JSON。SignalR ObjC包裝響應?

這就是我的迴應的片看起來像:

RESPONSE: { 
    A =  (
     "{\"NotificationType\":1,\"TelemetryDetails\":{\"serialNumber\":\"xxx\",\"name\":\"xxx\",,\"protectedDeviceIp\":\"xxx\"},{\"endpoint\":\"xxx\",\"ip\":\"xxx\",\"domain\":null,\"latitude\":null,\"longitude\":null,\"protectedDeviceId\":\"xxx\",\"protectedDeviceIp\":\"xxx\"}]}]},\"CommandResult\":null,\"FortressBoxSerialNumber\":\"xxx\"}" 
    ); 
    H = NotificationsHub; 
    M = notificationData; 
} 

在所有其它平臺,我的回答是隻什麼「A」的價值就在這裏。不知道爲什麼SignalR-ObjC將所有這些額外的數據(集線器信息)包裝在響應中。

我的代碼:

-(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 *strData) { 

       NSLog(@"RESPONSE: %@", strData); 

       NSError *jsonError; 
       NSData *objectData = [strData dataUsingEncoding:NSUTF8StringEncoding]; 
       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData 
                    options:NSJSONReadingMutableContainers 
                     error:&jsonError]; 

       NSLog(@"JSON DATA - %@", json); 

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

     }]; 
    }]; 
} 

如果我複製和在我的迴應粘貼對象從「A」的值到其中被編碼「strData是」,代碼解析它工作正常。但是,如果我傳遞整個響應對象,就會中斷。

考慮到我的響應對象看起來是一個字符串,我如何提取「A」的值或停止使用這些額外數據包裝我的響應的SignalR-ObjC?

+0

請發表大部分代碼,您如何收到該信封?我使用以下代碼並收到包含信封的NSDictionar: _hubConnection = [[SRHubConnection alloc] initWithURLString:url useDefault:YES]; _hubProxy = [_hubConnection createHubProxy:@「SomeHub」]; [_hubProxy on:@「ReceiveEnvelope」執行:self selector:@selector(onReceiveEnvelope :)]; –

+0

更多附加程序碼@NickolayOlshevsky – arcade16

回答

0

通過改變setReceived到hubConnection解決我的問題如下:

[hubConnection setReceived:^(NSDictionary *strData) { 

     NSLog(@"RESPONSE: %@", [strData valueForKey:@"A"]); 

     NSString *str = [strData valueForKey:@"A"][0]; 

     NSError *jsonError; 
     NSData *objectData = [str dataUsingEncoding:NSUTF8StringEncoding]; 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData 
                    options:NSJSONReadingMutableContainers 
                     error:&jsonError]; 

     NSLog(@"JSON DATA - %@", json); 

    }]; 

什麼解決的問題是從一個NSString的改變響應對象類型到的NSDictionary,然後意識到服用的「A」的一聲valueForKey我是一個數組,所以我必須在編碼/序列化之前獲得該數組中的第一個值。