解析 Swift 中的 JSON 数据 2

Parsing the JSON Data in Swift 2

我正在解析数据,我正在获取 responseString also.But 我的问题是我想将它转换成字典,然后我想从那个 jsonObject 中获取值。 但我无法得到那个。

我的代码如下

 func loginRequest(url:String, withParams params: [String: String?], postCompleted : (succeeded: Bool, msg: String) -> ()){

        let request = NSMutableURLRequest(URL: NSURL(string: url)!)

        let session = NSURLSession.sharedSession()

        request.HTTPMethod = "POST"



        var err: NSError?

        var bodyData = ""

        for (key,value) in params{

            if (value == nil){ continue }

            let scapedKey = key.stringByAddingPercentEncodingWithAllowedCharacters(

                .URLHostAllowedCharacterSet())!

            let scapedValue = value!.stringByAddingPercentEncodingWithAllowedCharacters(

                .URLHostAllowedCharacterSet())!

            bodyData += "\(scapedKey)=\(scapedValue)&"

        }



        request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)



        let task = session.dataTaskWithRequest(request,

            completionHandler: {data, response, error -> Void in

                let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)

                postCompleted(succeeded: true, msg: dataString! as String)

                print(dataString!)



                if let datas = dataString!.dataUsingEncoding(NSUTF8StringEncoding) {



                    do{

                        let json = try NSJSONSerialization.JSONObjectWithData(datas, options: []) as? [String:AnyObject]

                        //return json

                        print(json)

                    }

                    catch

                    {

                         print("Something went wrong")

                    }



                }





                          })



        task.resume()

    }

我是这样调用那个方法的

self.loginRequest("http://stream.gsr-india.com:8080/pgn_service/REST/WebService/GetUserDetails",

                withParams: ["first_name":firstNameTextField.text,"last_name":lastNameTextField.text , "application_id":uniqueIdTextField.text])

                {

                    (succeeded: Bool, msg: String) -> () in

                    if(succeeded) {

                        if msg == "0"

                        {

                            //Incorrect data...

                        }

                        else

                        {

                            //The login it's ok...



                        }

                    }

            }

谁能帮我解决这个问题。 提前致谢。

将方法声明更改为

 func loginRequest(url:String, withParams params: [String: String?], postCompleted : (succeeded: Bool, msg: NSDictionary?) -> ()){

然后将任务处理程序更改为

let task = session.dataTaskWithRequest(request,

    completionHandler: {data, response, error -> Void in

        let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
      print(dataString!)
           do{
               let json = try NSJSONSerialization.JSONObjectWithData(datas, options: []) as? [String:AnyObject]
               postCompleted(succeeded: true, msg: json)
                print(json)
           }
           catch {
                print("Something went wrong")
           }
       }
     })

将回调更改为

        (succeeded: Bool, msgDict: NSDictionary?) -> () in
                   if let dict =  msgDict {
                      // call any value like dict["error"] etc
                   }