0

i am developing an iOS app in Swift using Xcode. How can I get values of Json object from this HTTP response.I need values of inner message and status.

{"message":"success","statusCode":200,"data":{"message":"already logged in","status":0}}

Thank you in advance :)

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
M faz
  • 31
  • 1
  • 3

2 Answers2

1

First, let me recommend the best 3rd party API for REST calls (HTTP) which called Alamofire.

If you use the below Alamofire example:

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
     .responseJSON { response in
         print(response.request)  // original URL request
         print(response.response) // URL response
         print(response.data)     // server data
         print(response.result)   // result of response serialization

         // Parsing the JSON
         if let JSON = response.result.value {
             print("JSON: \(JSON)")

             // The actual cast to Dictionary
             let jsonDictionary = JSON as! Dictionary<String, AnyObject>
         }
     }

Basically jsonDictionary will be your JSON dictionary that the keys are String and the values are of type AnyObject (so you could cast to what ever you need) and now you can do the below calls:

let message = json["message"] as? String
OhadM
  • 4,687
  • 1
  • 47
  • 57
0

Get that dictionary and

let objDict = yourResponseObject.valueForKey("Data") as! NSDictionary

if objDict.valueforkey("message").isequaltoString("already logged in")

{

 User logged already

}

else

{

 not logged

}
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Nrv
  • 280
  • 1
  • 6