iOS Swift 4 /来自cloudant连接的响应

时间:2018-04-19 16:31:52

标签: swift parsing cloudant

目前我遇到的问题是我使用Cloudant和library来访问noSql数据库。

我也会得到回报。

这是订单:

    let read = GetAllDocsOperation(databaseName: dbName){ (response, httpInfo, error) in
        if let error = error {
            print("Encountered an error while reading a document. Error:\(error)")
        } else {

          print(response)

        }
    }

    client.add(operation:read)

以下是我的结果:

{{0}}

现在我不知道该怎么做。我首先尝试使用SwiftyJSON解析它或充当字典。

不幸的是,我失败了。

有人可以帮助我吗? 我是新人,请原谅我。

谢谢

1 个答案:

答案 0 :(得分:1)

首先,您需要安全地解包响应,然后访问JSON。这里的一个是手动完成所有操作,没有编码器,解码器或第三方

if let response = response as? [String: Any] {
    if let rowData = response["row"] as? [[String: Any]] {
        for row in rowData {
            if let id = row["id"] as? Int {
                print(id.description)
            } 
            if let key = row["key"] as? Int {
                print(key.description)
            }
            if let value = row["value"] as? [String: Any] {
                if let rev = value["rev"] as? String {
                     print(rev)
                }
            } 
        }
    }
}

但是我建议看一下ObjectMapper,它可以帮助你很多JSON,或者你可以尝试一下Apple的学习Encoding and Decoding