-2

I am new in Swift ,I am getting below JsonObject as a String,and I want to get LoginStatus's value, how can I parse it

{
   "ID":16,
   "NameSurname":"UĞUR ACAR",
   "LoginStatus":"True"
}

I am trying parse in here but it gives error

    func parser(_ parser: XMLParser, foundCharacters string: String) {
        if currentElementName == "LoginResult" {

            var status = ""

            do {
                let res = try JSONDecoder().decode(Root.self, from: Data(string.utf8))
                status = res.loginStatus

            } catch  {
                print("Unable to decode", error)
            }
}

1 Answer 1

4

You can try

struct Root: Codable {
    let id: Int
    let nameSurname, loginStatus: String

    enum CodingKeys: String, CodingKey {
        case id = "ID"
        case nameSurname = "NameSurname"
        case loginStatus = "LoginStatus"
    }
}

    do {
        let res = try JSONDecoder().decode(Root.self, from: Data(jsonString.utf8))

    } catch  {
        print("Unable to decode", error)
    }
Sign up to request clarification or add additional context in comments.

3 Comments

this is xml parses sure string is a jsonString ?
I can't get value of loginStatus, I got as a jsonString
@Diego res.loginStatus would have the value in this code example.

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.