Ios 信号机ObjC包装响应?
我正在开发一个使用SignalR ObjC的应用程序。我已经建立了与服务器的连接,并且正在接收正确的数据,但是响应对象将我抛出。我试图将响应解析为JSON,然后是一个字典,但看起来SignalR ObjC正在用额外的数据包装响应,这阻止了我将其解析为JSON 这就是我的回答的一部分:Ios 信号机ObjC包装响应?,ios,objective-c,json,signalr,Ios,Objective C,Json,Signalr,我正在开发一个使用SignalR ObjC的应用程序。我已经建立了与服务器的连接,并且正在接收正确的数据,但是响应对象将我抛出。我试图将响应解析为JSON,然后是一个字典,但看起来SignalR ObjC正在用额外的数据包装响应,这阻止了我将其解析为JSON 这就是我的回答的一部分: RESPONSE: { A = ( "{\"NotificationType\":1,\"TelemetryDetails\":{\"serialNumber\":\"xxx\",\
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”或停止信号器ObjC的值?通过将接收到的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给了我一个数组,因此我必须在对数组进行编码/序列化之前获取该数组中的第一个值。通过将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给了我一个数组,因此我必须在编码/序列化该数组之前获取该数组中的第一个值。请发布大多数代码,您如何接收该信封?我使用以下代码并接收带有信封的NSDictionars:_hubConnection=[[SRHubConnection alloc]initWithURLString:url useDefault:YES]_hubProxy=[\u hubConnection createHubProxy:@“SomeHub”];[_hubbroxyon:@“ReceiveEnvelope”执行:自选择器:@选择器(onReceiveEnvelope:)];更多代码添加@Nickolayolshevsky请发布大多数代码,您如何收到该信封?我使用以下代码并接收带有信封的NSDictionars:_hubConnection=[[SRHubConnection alloc]initWithURLString:url useDefault:YES]_hubProxy=[\u hubConnection createHubProxy:@“SomeHub”];[_hubbroxyon:@“ReceiveEnvelope”执行:自选择器:@选择器(onReceiveEnvelope:)];添加了更多代码@NickolayOlshevsky