如何转换JSON字典字符串值迅速4

问题描述 投票:1回答:2

我是新来的斯威夫特,我开始用PHP服务器斯威夫特4项目。我用Alamofire的请求,并使用print()打印数据。这就是我得到:

{"error":false,"n":"Raghad"}

但是,当我想将其转换为字符串,返回""(空),当我转换为Boolean它正确返回值。

那么,如何解决这个问题?

let wJSON : JSON = JSON(response.result.value!)
print(wJSON["n"].stringValue)
print(wJSON["error"].boolValue)
json swift alamofire
2个回答
0
投票

使用Decodable简单的解决方案,定义符合Decodable协议你的字典一个struct

struct Reply: Decodable {
    let error: Bool
    let n: String
}

let data = response.data
do {
    let result = try JSONDecoder().decode(Reply.self, from: data)
    print("\(result.n) \(result.error)")
} catch {
    print(error)
}

0
投票

我改变responseStringresponseJSON

Alamofire.request(URL!方法:.POST,参数:PAR).validate(){responseJSON响应中,如果response.result.isSuccess {让wJSON:(!response.result.value)JSON = JSON

和它的工作

© www.soinside.com 2019 - 2024. All rights reserved.