简体   繁体   中英

How to get particular 'json' value in swift 5?

I want the 'success' value in json object but the problem is I'm getting whole json data I want only 'success' value to print

Here is my json`

{
   response = { 
                success = 1; 
                successmsg = "Successful Connection"; 
              };
}`

Here is my code in swift 5

    @IBAction func girisButtonTap(_ sender: Any) {
        var txtusername: String
        var txtpassword: String
        txtusername = usercodeText.text!
        txtpassword = passwordText.text!
        let Url = String(format: "http://10.10.10.53:8080/sahambl/rest/sahamblsrv/userlogin")
        guard let serviceUrl = URL(string: Url) else { return }
        let parameters: [String: Any] = [
            "request": [
                "xusercode" : "\(txtusername)",
                "xpassword": "\(txtpassword)"
            ]
        ]
        var request = URLRequest(url: serviceUrl)
        request.httpMethod = "POST"
        request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
            return
        }
        request.httpBody = httpBody
        request.timeoutInterval = 20
        let session = URLSession.shared
        struct ResponseJSON: Codable {
            let response: Response
        }
        struct Response: Codable {
            let success: Int
            let successmsg: String
        }
        session.dataTask(with: request) { (data, response, error) in
            if let response = response {
                print(response)
            }
            if let data = data {
                do {
                    let json = try JSONDecoder().decode(ResponseJSON.self, from: data)
                    print(json)
                    let successful = json.response.success == 1
                } catch {
                    print(error)
                }
                }
            }.resume()
    }
}

I would be grateful for any progress.

Use a model struct and Codable for parsing:

struct ResponseJSON: Codable {
    let response: Response
}

struct Response: Codable {
    // depending on what your JSON actually looks like, this could also be
    //   let success: Bool
    let success: Int

    let successmsg: String
}

session.dataTask(with: request) { data, response, error in
  if let response = response {
      print(response)
  }
  if let data = data {
    do {
      let json = try JSONDecoder().decode(ResponseJSON.self, from: data)
      print(json)

      // access the success property:
      let successful = json.response.success == 1 
      // leave off the "== 1" if it's a Bool
    } catch {
       print(error)
    }
  }
}.resume()


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