Swift3 如何在swift 3中解析alamofire json响应?

Swift3 如何在swift 3中解析alamofire json响应?,swift3,xcode8,Swift3,Xcode8,我是swift编程新手,我花了大量时间研究如何解析alamofire服务器调用的json响应。我的Json响应是 {"customer_info":[{"customer_id":"147","response_code":1}]} 我想访问这两个变量。我的swift代码是 请帮助您的代码正在正确解析。将以下代码添加到blog循环中,并获取第二个变量 if let response_code = blog["response_code"] as? Int { //Do somethin

我是swift编程新手,我花了大量时间研究如何解析alamofire服务器调用的json响应。我的Json响应是

{"customer_info":[{"customer_id":"147","response_code":1}]}
我想访问这两个变量。我的swift代码是
请帮助

您的代码正在正确解析。将以下代码添加到blog循环中,并获取第二个变量

if let response_code = blog["response_code"] as? Int {
    //Do something here
}
因此,您要查找的完整代码是

let str = "{\"customer_info\":[{\"customer_id\":\"147\",\"response_code\":1}]}"

let data = str.data(using: .utf8)

do {
    if let data = data,
        let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
        let blogs = json["customer_info"] as? [[String: Any]] {
        for blog in blogs {
            if let name = blog["customer_id"] as? String {
                print(name)
            }
            if let response_code = blog["response_code"] as? Int {
                print(response_code)
            }
        }
    }
} catch {
    print("Error deserializing JSON: \(error)")
}

我已经修改了代码,现在得到了结果

if let jsonDict = response.result.value as? [String:Any],

                        let dataArray = jsonDict["customer_info"] as? [[String:Any]]
                        {
                              let nameArray = dataArray.flatMap { $0["customer_id"] as? String }
                             let nameArray2 = dataArray.flatMap { $0["response_code"] as? Int }

                            if(dataArray.count>0)
                            {
                              //store return customer id and response code

                                let customer_id_received = nameArray[0]
                                let response_code_received = nameArray2[0]

                                if(response_code_received==1)
                                {
                                //proceed with storing customer id in global variable

                                    print(nameArray2[0])

                                }

为什么JSON中有括号?它的格式不正确不,我把它放错了地方,我的json输出是{“customer_info”:[{“customer_id”:“147”,“response_code”:1}]}可能重复我在这行上得到的错误如果让data=data,它说未解析的标识符dataya正确,我意识到它不正确,但我已经尝试了alamofire json选项,到目前为止还不起作用。。如果让jsonDict=response.result.value为,任何线索都会对我更改代码很有帮助?[String:Any],让dataArray=jsonDict[“customer_info”]as?[[String:Any]]{let dataArray2=jsonDict[“customer_id”]as?[[String:Any]]print(dataArray2)}在这段代码编译并运行之后,但我得到的结果是零print@Swan试试我最新的答案。它在我的代码中运行并正确打印出来。您有一个顶级字典,其数组作为customer\u info的值。在数组的每个元素中,都有一个字典。因此,您必须首先将其解析为dictionary,然后将其解析为dictionary数组,然后执行for循环,就像我的代码一样。如果让jsonDict=response.result.value as,我已经根据一个指南将alamofire的代码更改为这个?[String:Any],让dataArray=jsonDict[“customer_info”]as?[[String:Any]{let nameArray=dataArray.flatMap{$0[“customer\u id”]as?Int}let nameArray2=dataArray.flatMap{$0[“respose\u code”]as?Int}print(dataArray)}并以[[“response\u code”:1,“customer\u id”:155]的形式获得输出谢谢大家对我的指导,我真的很感激!
if let jsonDict = response.result.value as? [String:Any],

                        let dataArray = jsonDict["customer_info"] as? [[String:Any]]
                        {
                              let nameArray = dataArray.flatMap { $0["customer_id"] as? String }
                             let nameArray2 = dataArray.flatMap { $0["response_code"] as? Int }

                            if(dataArray.count>0)
                            {
                              //store return customer id and response code

                                let customer_id_received = nameArray[0]
                                let response_code_received = nameArray2[0]

                                if(response_code_received==1)
                                {
                                //proceed with storing customer id in global variable

                                    print(nameArray2[0])

                                }