简体   繁体   中英

How to get Json Response in swift 3.0?

I am geting JSON response in different format in XCode 8 as it use to appear in XCode 7.3

Because of that my app stopped responding. I am sharing the screenshot of my response.

This is the code I am using

func MobileNumber(_ notification : Notification){
        activityIndicator.stopAnimating()
        if let notificationDict = (notification as NSNotification).userInfo{
            print(notificationDict)
            if let dataDict = notificationDict["Data"] as? NSDictionary{
                print(dataDict)
                if let status = dataDict["response"] as? Int{
                    switch status{
                    case 1:
                        if let data = dataDict["data"] as? NSDictionary{
                            if let mobileNumber = data["phone"] as? String{
                                UserDefaults.standard.set(mobileNumber, forKey: DefaultsKey.phoneNumber.rawValue)
                                print(mobileNumber)
                            }
                            UpdateFrame1()
                        }

JSON Response :

Data]: 243 bytes
[Result]: SUCCESS: {
    data =     {
        "__v" = 0;
        "_id" = 5833e131de2fe2342b0d0410;
        code = "<null>";
        "created_at" = "2016-11-22T06:09:53.097Z";
        "expiry_date" = "2016-11-22T06:09:53.097Z";
        phone = 9********;
        status = active;
    };
    message = "Resending your unused auth code";
    response = 1;
}
[Timeline]: Timeline: { "Request Start Time": 501493300.746, "Initial Response Time": 501493302.862, "Request Completed Time": 501493302.868, "Serialization Completed Time": 501493302.872, "Latency": 2.116 secs, "Request Duration": 2.122 secs, "Serialization Duration": 0.003 secs, "Total Duration": 2.126 secs }

Try this:-

let val = userInfo["SUCSESS"]
        if  let jsonData = (val as AnyObject).data(using: String.Encoding.utf8.rawValue)

        {
            do{
                let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers )

                if let dict = jsonObject as? [String: AnyObject]
                {

                    print("YOUR PHONE NUM \((dict as? [String : AnyObject])?["data"]?["phone"])")
                }

            }catch let error as NSError {
                print("Failed to load: \(error.localizedDescription)")


            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM