gpt4 book ai didi

ios - 无法使用 JSONJoy 解析来自 Socket.IO 的 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 08:56:38 24 4
gpt4 key购买 nike

我正在尝试使用 JSONJoy 转换从 Socket.IO 接收到的 JSON。我有以下 JSONJoy 协议(protocol)结构

struct Order: JSONJoy {
let location: String?
let username: String?

init(_ decoder: JSONDecoder) {
location = decoder["location"].string
username = decoder["username"].string
}
}

我的Socket是这样设置的

socket.on("order") {data, ack in
let order = Order(JSONDecoder(data))
print(order)
}

响应总是

Order(location: nil, username: nil)

订单是这样发出的

var order = JSON.stringify("{ 'username': 'Test', 'location': 'Start'}");
localSocket.emit('order', order);

我能够确认 iOS 和服务器端点上的连接。

最佳答案

您正在对字符串调用 JSON.stringify。要么只发出字符串,要么将对象字符串化。

例如

var order = JSON.stringify({username: 'Test', location: 'Start'});

关于ios - 无法使用 JSONJoy 解析来自 Socket.IO 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32899073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com