我有错误Use of unresolved identifier 'json'
。刚才我用的是迅捷4。
我只想获取json数据并基于返回参数返回3类型的msg。
快速消息(Fast msg)表示“已经保存”。
第二条消息(マイクーーンに追加されました)表示“现在保存”。
第三条消息(マイクーーンに追加できませんでした)表示“应用程序无法保存”。>这是因为缺少已注册的用户参数。
如何在swift4中解决Use of unresolved identifier 'json'
问题??
@objc func saveCouponToMyCoupon() {
let params = [
"merchant_id" : ApiService.sharedInstance.merchant_id,
"coupon_id" : self.coupon?.coupon_id
] as! [String : String]
Alamofire.request(APIURL.k_Coupon_Publish, method: .post, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: ApiService.sharedInstance.header)
.validate(statusCode: 200..<500)
.responseJSON { response in
switch response.result {
case .success(let data):
print(response)
print(response.result)
if json["returnCode"] == "E70" {
ErrorMessage.sharedIntance.show(title: "このクーポンは取得済です。", message: "")
}else {
ErrorMessage.sharedIntance.show(title: "マイクーポンに追加されました", message: "")
}
case .failure(let error):
debugPrint(error)
ErrorMessage.sharedIntance.show(title: "マイクーポンに追加できませんでした", message: "")
break
}
}
}
最佳答案
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
在您的例子中,
switch response.result … case .success(let data):
是获取response.result.value
中数据的另一种方法。但是,您将变量命名为
data
,而不是json
。如果你改了名字,它会起作用的。Alamofire.request(APIURL.k_Coupon_Publish, method: .post, parameters: params, encoding: URLEncoding(destination: .httpBody), headers: ApiService.sharedInstance.header)
.validate(statusCode: 200..<500)
.responseJSON { response in
switch response.result {
case .success(let json): // <-- use json instead data.
print(response)
print(response.result)
// Cast json to a string/any dictionary.
// Get the return code and cast it as a string.
// Finally, compare the return code to "E70".
if let dict = json as? [String: Any], let code = dict["returnCode"] as? String, code == "E70" {
ErrorMessage.sharedIntance.show(title: "このクーポンは取得済です。", message: "")
}else {
ErrorMessage.sharedIntance.show(title: "マイクーポンに追加されました", message: "")
}
case .failure(let error):
debugPrint(error)
ErrorMessage.sharedIntance.show(title: "マイクーポンに追加できませんでした", message: "")
break
}
}
关于json - 在swift4中使用未解析的标识符'json',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49096003/