json - Alamofire中的responseJSON json解析
问题描述
我正在获取 json 表单服务器。但是 json 迅速解析 nil。这是我的json
{
"out_code":"0",
"out_message":"0",
}
我尝试使用 Alamofire 和 swiftjson 将下面的代码输出到 out_code
func getData(){
Alamofire.request("url", method: .post,parameters: [
"request_code":"11"
]).responseJSON{(responseData) -> Void in
if((responseData.result.value != nil)){
let swiftyJsonVar = JSON(responseData.result.value!)
print("swiftyJsonVar==",swiftyJsonVar)
if let v_id = swiftyJsonVar["out_code"] as? String {
print("out_code----",v_id)
}
}
}
}
swiftyJsonVar==打印如下
{
"out_code" : "0",
"out_message" : null
}
但我没有从out_code键中获取数据。请帮我看看我的代码有什么问题...
解决方案
显然您正在使用 SwiftyJSON 库。
每种类型的转换都是从库中处理的。尝试使用以下代码而不是强制转换为String
:
if let v_id = swiftyJsonVar["out_code"].string {
print("out_code----",v_id)
}
推荐阅读
- centos - OpenLDAP 策略翻译
- javascript - 更改多个字体颜色
- bash - 将 linux 命令移动到 shell 脚本中
- django - ASGI vs WSGI,WSGI 兼容性问题
- pandas - 加入两个数据帧并使用数据帧的索引获取新帧
- zsh - oh-my-zsh:找不到插件“git-remote-branch”
- python - 通过分组使两个数组大小相同
- php - Laravel api 错误 405 方法不允许 + POST 方法被服务器接收为 GET
- google-apps-script - 粘贴的值是否被识别为作为 onEdit 函数参数的事件对象?
- python - 在 Jupyter 笔记本中使用 PySpark 读取 XML