SRMR
SRMR

Reputation: 3134

Swift 4 Codable decoding json

I'm trying to implement the new Codable protocol, so I added Codable to my struct, but am stuck on decoding the JSON.

Here's what I had before:

Struct -

struct Question {
    var title: String
    var answer: Int
    var question: Int
}

Client -

...

guard let data = data else {
    return
}

do {
    self.jsonResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
    let questionItems = self.jsonResponse?["themes"] as! [[String: Any]]

    questionItems.forEach {
        let item = Question(title: $0["title"] as! String,
                            answer: $0["answer"] as! Int,
                            question: $0["question"] as! Int)
        questionData.append(item)
    }

} catch {
    print("error")
}

Here's what I have now, except I can't figure out the decoder part:

Struct -

struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}

Client -

...

let decoder = JSONDecoder()
if let questions = try? decoder.decode([Question].self, from: data) {
    // Can't get past this part
} else {
    print("Not working")
}

It prints "Not working" because I can't get past the decoder.decode part. Any ideas? Will post any extra code as needed, thanks!

EDIT:

Sample of API JSON:

{
  "themes": [
    {
      "answer": 1,
      "question": 44438222,
      "title": "How many letters are in the alphabet?"
    },
    {
      "answer": 0,
      "question": 44438489,
      "title": "This is a random question"
    }
  ]
 }

If I print self.jsonResponse I get this:

Optional(["themes": <__NSArrayI 0x6180002478f0>(
{
    "answer" = 7;
    "question" = 7674790;
    title = "This is the title of the question";
},
{
    "answer_" = 2;
    "question" = 23915741;
    title = "This is the title of the question";
}

My new code:

struct Theme: Codable {
    var themes : [Question]
}

struct Question: Codable {
    var title: String
    var answer: Int
    var question: Int
}

...

if let decoded = try? JSONDecoder().decode(Theme.self, from: data) {
    print("decoded:", decoded)
} else {
    print("Not working")
}

Upvotes: 7

Views: 5202

Answers (3)

CrazyPro007
CrazyPro007

Reputation: 1062

Hello @all I have added the code for JSON Encoding and Decoding for Swift 4.

Please use the link here

Upvotes: 0

vadian
vadian

Reputation: 285039

If your JSON has a structure

{"themes" : [{"title": "Foo", "answer": 1, "question": 2},
             {"title": "Bar", "answer": 3, "question": 4}]}

you need an equivalent for the themes object. Add this struct

struct Theme : Codable {
    var themes : [Question]
}

Now you can decode the JSON:

if let decoded = try? JSONDecoder().decode(Theme.self, from: data) {
    print("decoded:", decoded)
} else {
    print("Not working")
}

The containing Question objects are decoded implicitly.

Upvotes: 8

Itai Ferber
Itai Ferber

Reputation: 29764

You're getting this error because your JSON is likely structured as so:

{
  "themes": [
    { "title": ..., "question": ..., "answer": ... },
    { "title": ..., "question": ..., "answer": ... },
    { ... }
  ],
  ...
}

However, the code you wrote expects a [Question] at the top level. What you need is a different top-level type that has a themes property which is a [Question]. When you decode that top-level type, your [Question] will be decoded for the themes key.

Upvotes: 1

Related Questions