As of now am parsing the JSON data directly from array,without model class. Please suggest the efficient way to parse JSON either to use model class or directly parse JSON data.
-
1you can use swift's Codable feature to Decode JSON. You can look at [QuickType.io](https://app.quicktype.io/) to generate code for JSON Result – Lal Krishna Jul 20 '18 at 07:34
-
try https://github.com/Ahmed-Ali/JSONExport this for convert your JSON object to model – Shahrukh Jul 20 '18 at 07:47
2 Answers
If you are using swift 4 then apple has some new classes and protocols we can use JSONDecoder and Decodable for decoding from JSON data to a model object, and JSONEncoder and Encodable for encoding from a model object to JSON data.

- 41
- 5
As per Apple developer documentation
The Swift standard library defines a standardized approach to data encoding and decoding. You adopt this approach by implementing the Encodable and Decodable protocols on your custom types. Adopting these protocols lets implementations of the Encoder and Decoder protocols take your data and encode or decode it to and from an external representation such as JSON or property list.
For json decoding you can use Struct and classes which implements Decodable protocol. Below is the simple example from developer site
struct Landmark: Decodable {
var name: String
var foundingYear: Int
}
now you can create the Landmark struct from your JSON data like
do {
let landmark = try JSONDecoder().decode(Landmark.self, from: data)
} catch {
print(error.localizedDescription)
}
Apart from Codable protocol there are many third party libraries available like ObjectMapper, SwiftyJSON etc. but the recommended way is to try Codable
protocol first before to thirs party solutions.

- 11,748
- 3
- 50
- 60