Swift嵌套jSON解码

时间:2020-05-15 13:55:31

标签: ios json swift dictionary

我在嵌套json解码方面遇到问题。我没有收到任何错误,但响应为空{}。下面是我的示例json和struct。

{
"categories": [
{
  "ID": 130,
  "data": [
    {
      "en": [
        {
          "title": "test"
        }
      ],
      "fr": [
        {
          "title": "teste"
        }
      ]
    }
  ],
  "lifts": [
    {
      "ID": 104,
      "data": [
        {
          "en": [
            {
              "code": "test",
              "title": "test"
            }
          ],
          "fr": [
            {
              "code": "test",
              "title": "test"
            }
          ]
        }
      ]
    },
    {
      "ID": 105,
      "data": [
        {
          "en": [
            {
              "code": "test",
              "title": "test"
            }
          ],
          "fr": [
            {
              "code": "test",
              "title": "test"
            }
          ]
        }
      ]
    }
  ]
}

这是我的结构

struct jsonResponse : Codable {

struct Categories : Codable {

    let id : Int
    let data : [LanguageData]
    let lifts : [Lifts]

    struct LanguageData : Codable {

        let en, fr : [Data]

        struct Data : Codable {

            let code : String?
            let title : String?
        }
    }

    struct LiftsData : Codable {
        let id : Int
        let data : [LanguageData]
    }
}

然后,我试图像这样解码JSON:

let lifts = try JSONDecoder().decode(jsonResponse.self, from: data)

但是当我打印升降机时,我只看到空的{}。解码期间也没有错误消息,所以不知道可能是什么错误。

0 个答案:

没有答案