I am new in Swift 4 and trying to figure out How to convert Json to swift Object automatically like Gson in java. Is there is any plugin i can use which can convert my json to object and vice versa. I have tried to use SwiftyJson Library but couldnt understand what is syntax for directly converting the json to object mapper. In Gson conversion is as follow :
String jsonInString = gson.toJson(obj);
Staff staff = gson.fromJson(jsonInString, Staff.class);
Can you please suggest some really simple example for beginner like me . below is my swift person class :
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
below is method call to fetch response from server :
let response = Helper.makeHttpCall(url: "http://localhost:8080/HttpServices/GetBasicJson", method: "PUT", param: interestingNumbers)
In response variable I am getting json:
{
"firstName": "John",
"lastName": "doe"
}
Codable
protocol &JSONEncoder
/JSONDecoder
for this purpose. Here is a really comprehensive guide on this topic. – Shawana