1

Hey I am programming a swift app and I consume a rest service for my data. I consume it like this:

static func findAll() {
    let URL = baseURL + "api/person"

    Alamofire.request(URL).responseJSON {
        response in
        print(response.result.value ?? "")
    }
}

This is the json that is returned:

{
    Email = "[email protected]";
    Geburtsdatum = "0001-01-01T00:00:00";
    Nachname = Gnadlinger;
    Password = "<null>";
    PersonId = 0;
    Telefonnummer = 9832742;
    Username = SonnyBlackzz;
    Vorname = Johannes;
}

Is there a way to save these json values and parse them into an object?

Best regards!

1
  • 5
    This is not JSON. Commented Feb 28, 2017 at 10:53

1 Answer 1

2

Just create an object eg:

struct Person {

   var username: String
   var email: String

   init(username: String, email: String) {
       self.username = username
       self.email = email
   }
}

And when you get your data just do this:

Alamofire.request(URL).responseJSON {
        response in

     let json = response.result.value

     guard let json != nil else {
          return
     }

     let newPerson = Person(username: json["username"], email: json["email"])
}
Sign up to request clarification or add additional context in comments.

1 Comment

I see that you tag swifty-json, is a usefull library to handle json, but this code should work

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.