Swift Alamofire JSON 响应有括号



我正在尝试使用来自Alamofire请求的Alamofire处理JSON响应:

Alamofire.request(.POST, dbURL, parameters: ["username": username, "password": password]) .responseJSON { response in ... }

我正在阅读回复

let JSONResponse = response.result.value!

此值返回

({ keyOne = 2; keyTwo = 4; keyThree = 2; keyFour = 6;})

要从中获取数据,

JSONResponse.valueForKey("exampleKey")

得到的问题是这不返回简单的数据,它返回这个(响应都是数字,但由于明显的原因我不能使用 integerForKey):

(二)

(四)

(二)

(六)

如何在没有括号的情况下从此功能获取、读取和/或保存数据?我尝试使用stringByReplacingOccurencesOfString()蛮力移除支架,但这很丑陋,

首先不起作用。

如果你的 json 是这个{ "keyOne":2, "keyTwo":4, "keyThree":2, "keyFour":6 }然后,此代码应为您返回正确的 int 值。

if let jsonURL = NSBundle.mainBundle().URLForResource("sample", withExtension: "json")
{
    Alamofire.request(.GET, jsonURL, parameters:nil) .responseJSON {
        response in
        if let JSONResponse = response.result.value as? [String:Int]
        {
            print(JSONResponse)
            if let val = JSONResponse["keyOne"] {
            print ("val = (val)")
            }
        }
    }
}

最新更新