【问题标题】:Invalid type in JSON write (RTCIceCandidate)JSON 写入中的类型无效 (RTCIceCandidate)
【发布时间】:2021-08-08 09:48:00
【问题描述】:

我正在使用 websocket 来调用接收、接受和所有。接听电话时,我收到了 websocket 事件。

websocket 接受事件输出:-

{
    action = signal;
    "call_id" = 60a4d5b4850b3875f95dbc6e;
    event = CALL;
    from = 5b30ec0fa4cef4609038470b;
    id = "330A1A48-A6BA-4B29-A4A2-9D9FDB85144D";
    signal =     {
        offer = "RTC_OBJC_TYPE(RTCIceCandidate):\naudio\n0\ncandidate:1211696075 1 udp 41885439 3.8.66.208 62545 typ relay raddr 0.0.0.0 rport 0 generation 0 ufrag DT/j network-id 1 network-cost 10\nturn:3.8.66.208:3478?transport=udp";
    };
    to = 5b4854724f82e91934c1c475;
}

我正在使用下面的代码将上面的字典对象转换为 json 字符串。

-(NSString *)convertDictionaryToJsonString:(NSMutableDictionary *)dict {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

问题:- 应用程序崩溃了,原因是 - 'Invalid type in JSON write (RTCIceCandidate)'

谁能知道如何避免崩溃并解决这个问题?

谢谢。

【问题讨论】:

  • 您的字典看起来还不错。你能告诉更多关于dict 的信息(有一个RTCIceCandidate 对象吗?)dict[@"signal"][@"offer] 真的是NSString 还是自定义描述?
  • (RTCIceCandidate) - 它在“offer”键值中,但整个内容是一个字符串。所以它可以工作。
  • @Larme 我编辑了问题,并完整回复了dict
  • 该代码是否在您的 pod github.com/WebKit/WebKit/blob/… 中?否则,您能否检查每个属性的类型:NSLog(@"Class: %@", NSStringFromClass([dict[@"signal"][@"offer] class]));。我猜它正在返回RTCIceCandidate,因为那是它的descriptiongithub.com/WebKit/WebKit/blob/…
  • 好吧,让我把日志和检查

标签: ios json objective-c dictionary websocket


【解决方案1】:

以下是 JSON 的有效类型:

可以转换为 JSON 的 Foundation 对象必须具有以下属性:

  • 顶级对象是NSArrayNSDictionary
  • 所有对象都是NSStringNSNumberNSArrayNSDictionaryNSNull 的实例。
  • 所有字典键都是NSString 的实例。
  • 数字不是NaNinfinity

您的错误是说在某些时候,有一个对象不是允许的类型,它是RTCIceCandidate

看到"RTC_OBJC_TYPE(RTCIceCandidate):\naudio\n0\ncandidate:1211696075 1 udp 41885439 3.8.66.208 62545 typ relay raddr 0.0.0.0 rport 0 generation 0 ufrag DT/j network-id 1 network-cost 10\nturn:3.8.66.208:3478?transport=udp" 会让你觉得这确实是NSString 但是,如果我们看到RTCIceCandidate 的代码(因为它是罪魁祸首类),我们会看到覆盖description

- (NSString *)description {
  return [NSString stringWithFormat:@"RTCIceCandidate:\n%@\n%d\n%@\n%@",
                                    _sdpMid,
                                    _sdpMLineIndex,
                                    _sdp,
                                    _serverUrl];
}

有一个RTCIceCandidate+JSON extension 提供了一些有趣的方法将RTCIceCandidate 转换为 JSONString/JSONData。

您需要将罪魁祸首对象替换为允许类的对象。

RTCIceCandidate *candidate = dict["signal"]["offer"];
NSString/NSDictionary *candidateValid = //Whatever the method you want with a method from RTCIceCandidate+JSON, custom one ?

dict[@"signal"] = @{@"offer": candidateValid}; //Because I guess that dict[@"signal"] is in a fact a `NSDictionary` and not a `NSMutableDictionary`

//Current code

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2019-10-26
    • 2015-03-19
    • 2016-03-16
    • 2021-12-26
    • 2017-07-05
    • 2017-03-05
    • 2017-01-24
    相关资源
    最近更新 更多