Convert between JSON and Model objects
masterOnce a type implements Mappable, you can convert between JSON strings and model instances using either the model's convenience methods or the Mapper class.
Using Model Convenience Methods:
- Convert JSON string to object:
User(JSONString: JSONString) - Convert object to JSON string:
user.toJSONString(prettyPrint: true)
Using the Mapper class:
- Convert JSON string to object:
Mapper<User>().map(JSONString: JSONString) - Convert object to JSON string:
Mapper().toJSONString(user, prettyPrint: true)
// Convert JSON String to Model
let user = Mapper<User>().map(JSONString: JSONString)
// Create JSON String from Model
let JSONString = Mapper().toJSONString(user, prettyPrint: true)