2017-03-09 6 views
1

私はSignalR-ObjCを使用するアプリケーションで作業しています。私はサーバーへの接続を設定して、正しいデータを受信して​​いますが、応答オブジェクトが私を捨てています。私はJSONとそれから辞書への応答を解析しようとしていますが、SignalR-ObjCが応答をJSONへの解析を妨げる余分なデータにラッピングしているようです。SignalR ObjCのラッピング応答?

これは私の応答のスライスは次のようになります。私の回答は、「A」の値がここにあるものだけで、他のすべてのプラットフォームで

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

。なぜ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]; 

     }]; 
    }]; 
} 

私はコピーして、「strData」はエンコードされている場所に私の応答オブジェクトの「A」から値を貼り付けた場合は、それを解析するためのコードが正常に動作します。しかし、応答オブジェクト全体を渡すと、応答オブジェクトが破損します。

私の応答オブジェクトは文字列であるように見えますが、 "A"の値を抽出するか、SignalR-ObjCがこの余分なデータで応答をラップするのを止めるにはどうすればいいですか? hubConnectionを変更することで、私の問題を解決しました

+0

ほとんどのコードを投稿してください。どのように受信しますか?私は次のコードを使用して、エンベロープでNSDictionarを受け取ります: _hubConnection = [[SRHubConnection alloc] initWithURLString:url useDefault:YES]; _hubProxy = [_hubConnection createHubProxy:@ "SomeHub"]; [_hubProxy on:@ "ReceiveEnvelope" perform:自己セレクタ:@selector(onReceiveEnvelope :)]; –

+0

さらに詳しいコードが追加されました@NickolayOlshevsky – arcade16

答えて

0

は、次のようにsetReceived:

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

    }]; 

を問題はNSDictionaryのにNSStringのから応答オブジェクトの種類を変更して解決何、それから「A」与えたのvalueForKeyを取っ実現私は配列ですので、配列の最初の値をエンコード/シリアル化する前に取得しなければなりませんでした。