2

I want to get the array from the JSON Object

Here is my Code:

let url = URL(string:"http://192.168.0.117/rest/login.php")
        let parameters = ["email": email , "pwd":password]
        var request = URLRequest(url : url!)
        request.httpMethod = "POST"
        request.httpBody = try? JSONSerialization.data(withJSONObject:parameters, options: [])
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        let session = URLSession.shared
        session.dataTask(with: request, completionHandler: { (data, response, error) in
            if let data = data {
                do {
                    let json = try? JSONSerialization.jsonObject(with: data, options: []) as! Dictionary<String, Any>
                    if let json = json {
                        print("HERE SHOULD BE YOUR JSON OF LOGIN : \(json)")
                        let status = json["status"] as! String
                        let datas = json["data"] as! String
                        print("Here is the Data : \(datas)")                       
                        print("here LOIGN : \(status)")
                        if status == "200"
                        {
                            DispatchQueue.main.async
                                {
                                    self.performSegue(withIdentifier: "dosigninEmp", sender: self)
                            }
                        } else if status == "204"
                        {
                            self.displayMessage(userMessage: "Not Authorized User")
                        }
                    }
                }
            } else {
                print("Error \(String(describing: error?.localizedDescription))")
            }
        }).resume()

I am getting the Output as:

HERE SHOULD BE YOUR JSON OF LOGIN : ["status": 200, "data": {
    "emp_id" = 1004;
    type = emp;
}, "Message": Auth Succesful]

how i can get "type" because i am getting status and using segue but now i want to fetch status as well as type to use segue and i am unable to get type Any Help would be appreciated

7
  • try this line to parse json :- let json = try? JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] Commented Apr 2, 2018 at 7:04
  • 2
    YOur json response is in either dict or in array, so what does semicolon is doing there?? Commented Apr 2, 2018 at 7:05
  • @sohanvanani. [String: Any] and Dictionary<String, Any> is exactly the same thing. Commented Apr 2, 2018 at 7:07
  • it is in array . i am getting this response only . but in android i am getting the proper response. @dahiya_boy Commented Apr 2, 2018 at 7:10
  • @Abhijit Show me your postman screen shot for this api response bcz i don't think your response is proper. pls recheck it. Commented Apr 2, 2018 at 7:14

1 Answer 1

1

I think converting your data in json will give proper json format of string, array & dictionaries. And that's why it would give the datas in Dictionary:

print("HERE SHOULD BE YOUR JSON OF LOGIN : \(json)")
let status = json["status"] as! String
let datas = json["data"] as! String
print("Here is the Data : \(datas)")                       
print("here LOIGN : \(status)")

Can change these lines into:

var type = ""
var datas = [String: AnyObject]()
if let data = json["data"] as? [String: AnyObject], let typeStr = data["type"] as? String {
    datas = data
    type = typeStr
}
print("Here is the Data : \(datas)")                       
print("Here Type : \(type)")
Sign up to request clarification or add additional context in comments.

3 Comments

@Abhijit, one more thing, perhaps you will receive status in NSNumber not in string, so to get that correct use let status = String(json["status"] as! NSNumber). Hope this will help
I am getting the value of the status using string only . because the server is sending the status in string itself @Ankit
@Abhijit. Ok, thats fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.