Ios Swift解码JSON-无法解码

Ios Swift解码JSON-无法解码,ios,json,swift,decode,Ios,Json,Swift,Decode,我无法解码我的JSON文件。如果我只解码一个字符串,它就可以工作,但是现在用我的struct它就不工作了。我做错什么了吗 我要解码的结构: struct Comment: Decodable, Identifiable { var id = UUID() var title : String var comments : [String] private enum Keys: String, CodingKey { case response =

我无法解码我的JSON文件。如果我只解码一个字符串,它就可以工作,但是现在用我的struct它就不工作了。我做错什么了吗

我要解码的结构:

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)!
    }
}
我的JSON:

{"Response": {
    "commentsArray":[
      {
        "title": "someTitle",
        "comments": [
          "optionOne",
          "optionTwo"
        ]
      },
      {
        "title": "title",
        "comments": [
          "optionOne",
          "optionTwo"
        ]
      },
      {
        "title": "someto",
        "comments": [
          "optionOne",
          "optionTwo"
        ]
      }
    ]
    }
  }

使用这些结构来解码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]
}

使用这些结构来解码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]
}

我认为您不希望以这种方式构造代码。您有一个层次结构,您正试图在一个更平坦的结构中捕获它

Response
    CommentsList
        Comments
            Title
            Comment
            Comment
            ...
        Comments
        ...
因此,您可能希望执行以下操作:

struct响应:可解码{
var commentsArray:[注释]
init(来自解码器:解码器){
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)
}
枚举响应编码键:字符串,编码键{case response=“response”}
枚举列表编码键:字符串,编码键{case commentsArray}
}
结构注释:可解码{
var id=UUID()
变量标题:字符串
var注释:[字符串]
}

我认为您不希望以这种方式构造代码。您有一个层次结构,您正试图在一个更平坦的结构中捕获它

Response
    CommentsList
        Comments
            Title
            Comment
            Comment
            ...
        Comments
        ...
因此,您可能希望执行以下操作:

struct响应:可解码{
var commentsArray:[注释]
init(来自解码器:解码器){
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)
}
枚举响应编码键:字符串,编码键{case response=“response”}
枚举列表编码键:字符串,编码键{case commentsArray}
}
结构注释:可解码{
var id=UUID()
变量标题:字符串
var注释:[字符串]
}

您遇到了什么错误?您遇到了什么错误?