Question: How do I unmarshal JSON onto a swift class? Here is [Go code](http://golang.org/pkg/encoding/json/#example_Unmarshal) which illustrates what I am after.

I can't find any reference which shows how to do this in Swift. A reference I looked at ( among others ) was this [tutorial](http://www.binpress.com/tutorial/swiftyjson-how-to-handle-json-in-swift/111).

But those example uses dictionaries. I want to use objects.

I saw attempts around this la: <http://bit.ly/1t3W2Gi>

but that post does not unmarshal to a class.

Answer: 
Nowadays, you’d do this with `Codable` types and `JSONDecoder`.

```
struct Animal: Codable {
    let type: String
    let sound: String
    let legs: Int
}

let json = """
    [{"type": "dog", "sound": "woof", "legs": 4},
    {"type": "cat", "sound": "meow", "legs": 4},
    {"type": "snake", "sound": "hiss", "legs": 0}]
    """

let data = json.data(using: .utf8)!

do {
    let animals = try JSONDecoder().decode([Animal].self, from: data)
    print(animals)
} catch {
    print("json failed: \(error)")
}

```

For more information, see:

* [Encoding and Decoding Custom Types](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types)
* Using [JSON with Custom Types](https://developer.apple.com/documentation/foundation/archives_and_serialization/using_json_with_custom_types) sample code.

---

The above is for Swift 3 and later. See the [previous revision](https://stackoverflow.com/revisions/26490922/2) of this a answer, predating `JSONDecoder` for earlier versions of Swift.
I hope the following link will be helpful for the usage of "Object Mapper"

```
[https://github.com/Hearst-DD/ObjectMapper][1]

```