【问题标题】:SignalR ObjC wrapping response?SignalR ObjC 包装响应?
【发布时间】:2017-03-09 14:52:45
【问题描述】:

我正在开发一个使用 SignalR-ObjC 的应用程序。我已经建立了到我们服务器的连接并且正在接收正确的数据,但是响应对象让我失望了。我正在尝试解析对 JSON 的响应,然后解析字典,但看起来 SignalR-ObjC 正在将响应包装在额外的数据中,从而阻止我将其解析为 JSON。

这是我的部分回复的样子:

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 使用这些额外数据包装我的响应?

【问题讨论】:

  • 请张贴大部分代码,您如何收到那个信封?我使用以下代码并接收带有信封的 NSDictionar:_hubConnection = [[SRHubConnection alloc] initWithURLString:url useDefault:YES]; _hubProxy = [_hubConnection createHubProxy:@"SomeHub"]; [_hubProxy on:@"ReceiveEnvelope" perform:self 选择器:@selector(onReceiveEnvelope:)];
  • 添加了更多代码@NickolayOlshevsky

标签: ios objective-c json signalr


【解决方案1】:

通过将 hubConnection 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);

   }];

解决问题的方法是将响应对象类型从 NSString 更改为 NSDictionary,然后意识到取“A”的 valueForKey 给了我一个数组,所以我必须在编码/序列化之前获取该数组中的第一个值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2022-01-14
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 2014-10-10
    相关资源
    最近更新 更多