【问题标题】:SwiftyJSON not working with WebSocket messageSwiftyJSON 不适用于 WebSocket 消息
【发布时间】:2020-08-13 00:16:00
【问题描述】:

我有一个这样的套接字响应:

{"op":0,"d":{"author":{"id":"6699457769390473216","name":"Test","verified":false},"unixTime":1597277057132,"id":"6699465549836976128","group":"64632423765273287342","content":"Yo","_id":"5f34838198980c0023fa49e3"},"t":"MESSAGE"}

我需要访问“d”对象,我已经尝试过

print(JSON(data)["d"])

它每次都返回null

【问题讨论】:

标签: json swift websocket swifty-json


【解决方案1】:

如果data 是String 类型,您可能使用了错误的init 方法来初始化JSON 对象。尝试像这样使用init(parseJSON:)

let jsonString = """
{
    "op": 0,
    "d": {
        "author": {
            "id": "6699457769390473216",
            "name": "Test",
            "verified": false
        },
        "unixTime": 1597277057132,
        "id": "6699465549836976128",
        "group": "64632423765273287342",
        "content": "Yo",
        "_id": "5f34838198980c0023fa49e3"
    },
    "t": "MESSAGE"
}
"""
let json = JSON(parseJSON: jsonString)

print(json["d"])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多