0

I can not decode my JSON File. It works if I decode only a single string but now with my struct it does not work. Is there anything that I do wrong?

My struct that i want to decode:

struct Comment: Decodable, Identifiable {
    var id = UUID()
    var title : String
    var comments : [String]

    private enum Keys: String, CodingKey {
        case response = "Response"
        case commentsArray = "commentsArray"
        case title = "title"
        case comments = "comments"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: Keys.self)
        let response = try values.nestedContainer(keyedBy: Keys.self, forKey: .response)
        let commentsArray = try response.nestedContainer(keyedBy: Keys.self, forKey: .commentsArray)
        title = try commentsArray.decodeIfPresent(String.self, forKey: .title)!
        comments = try commentsArray.decodeIfPresent([String].self, forKey: .comments)!
    }
}

My JSON:

{"Response": {
    "commentsArray":[
      {
        "title": "someTitle",
        "comments": [
          "optionOne",
          "optionTwo"
        ]
      },
      {
        "title": "title",
        "comments": [
          "optionOne",
          "optionTwo"
        ]
      },
      {
        "title": "someto",
        "comments": [
          "optionOne",
          "optionTwo"
        ]
      }
    ]
    }
  }
1
  • What error did you get? Commented Feb 11, 2020 at 18:02

2 Answers 2

2

use these structs to decode your json

struct Response: Codable {
    var response : Comments

    private enum Keys: String, CodingKey {
      case response = "Response"
    }
}
struct Comments: Codable {
    var commentsArray : [comment]
}
struct comment: Codable {
    let title: String
    let comments: [String]
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't think that you want to structure your code this way. You have a hierarchy that you are attempting to capture in a flatter structure.

Response
    CommentsList
        Comments
            Title
            Comment
            Comment
            ...
        Comments
        ...

So, you might want to do something like this:

struct Response: Decodable {
    var commentsArray: [Comments]

    init(from decoder: Decoder) {
        let container = try! decoder.container(keyedBy: ResponseCodingKeys.self)
        let response = try! container.nestedContainer(keyedBy: ListCodingKeys.self, forKey: .response)
        commentsArray = try! response.decode([Comments].self, forKey: .commentsArray)
    }

    enum ResponseCodingKeys: String, CodingKey { case response = "Response" }
    enum ListCodingKeys: String, CodingKey { case commentsArray }
}

struct Comments: Decodable {
    var id = UUID()
    var title: String
    var comments: [String]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.