Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil)

57 Views Asked by At

I have an error: Expected to decode Array but found a dictionary instead.", underlyingError: nil)

I use Moya and have JSON:

"buttons" : [
  {
    "name" : "chat",
    "title" : "Сообщения",
    "active" : true
  },
  {
    "title" : "Управление заявкой",
    "active" : true,
    "name" : "posterRecordActions",
    "actions" : {
      "name" : "cancelPosterRecord",
      "title" : "Отменить заявку",
      "active" : true
    }
  }
]

My Struct:

struct Record: Codable {
        let id: Int
        let title: String
        @FailableURL var image: URL?
        let statusTitle: String
        let statusColorName: String
        let price: Int
        let startTime: Int
        let description: String?
        let placeTitle: String
        let members: [Members]
        let rules: String?
        let recordForm: [String]
        let buttons: [API.Posters.RequestButtons]

 struct RequestButtons: Codable {
        let name: RequestButtonName
        let title: String
        let active: Bool?
        let actions: [RequestButtons]?
        let paymentLink: String?
        let paymentContext: String?
        let value: String?
        let badge: Int?
    }

I think, they problems with actions inside buttons, but I can't resolve this(

1

There are 1 best solutions below

0
Mohammad Reza Ansari On

Actions in your JSON is NOT array, it's a dictionary.

Change your struct like below code:

 struct RequestButtons: Codable {
    ...
    let actions: RequestButtons?
    ...
}

Additionally, I suggest to read swift API design for your naming convention; API Design Guidelines

For example RequestButtons have to be RequestButton or actions in your case have to be action, because they're not plural