0

I want to decode a JSON file into model objects. Unfortunately it doesn't work in the right way. So I don't get errors, but the "decoding-result" don't corresponds to my expectations. I have the following JSON file and I want to decode it in the shown structs. I have trimmed the json file. Why get I just one "slider image" instead of 5 (the property image of ImagesSlider contains an array with just the first image/element). What am I missing?

JSON:

[{"imageSlider" : [{
        "image" : [{
             "imageId" : "1",
             "imageName" : "germany1",
             "imageBigName" : "germany1_BIG",
             "imageRights" : "Peter"
        }],
        "image" : [{
             "imageId" : "2",
             "imageName" : "germany2",
             "imageBigName" : "germany2_BIG",
             "imageRights" : "Peter"
        }],
        "image" : [{
             "imageId" : "3",
             "imageName" : "germany3",
             "imageBigName" : "germany3_BIG",
             "imageRights" : "Peter"
        }],
        "image" : [{
             "imageId" : "4",
             "imageName" : "germany4",
             "imageBigName" : "germany4_BIG",
             "imageRights" : "Peter"
        }],
        "image" : [{
             "imageId" : "5",
             "imageName" : "germany5",
             "imageBigName" : "germany5_BIG",
             "imageRights" : "Peter"
        }]
    }]
}]

Swift:

struct CountryModel : Decodable, Equatable {    
    var countryName : String
    var inhabitants : String
    var capital : String
    var currency : String
    var imageName : String
    var imageSlider: [ImagesSlider]
}

struct ImagesSlider : Decodable, Equatable {
    var image: [Image]
}

struct Image : Decodable, Equatable {
    var imageId: String
    var imageName: String
    var imageBigName: String
    var imageRights: String
}

Decoding:

func loadData() -> [CountryModel] {
    var data: Data

    guard let file = Bundle.main.url(forResource: "data", withExtension: "json") else {
        fatalError("Error")
    }

    data = try! Data(contentsOf: file)
    let decoder = JSONDecoder()
    return try! decoder.decode([CountryModel].self, from: data)

}

Thanks for your help...

Edit: My question isn't solved by the linked question...

finebel
  • 2,227
  • 1
  • 9
  • 20
  • 1
    Possible duplicate of [Simple and clean way to convert JSON string to Object in Swift](https://stackoverflow.com/questions/25621120/simple-and-clean-way-to-convert-json-string-to-object-in-swift) – Anastasios Moraitis Oct 05 '19 at 18:16

2 Answers2

0

The json looks wrong, remember that you should have only one unique key per json object. Right now is:

[
  {
    "imageSlider": [
      {
        "image": [
          {
            "imageId": "1",
            "imageName": "germany1",
            "imageBigName": "germany1_BIG",
            "imageRights": "Peter"
          }
        ],
        "image": [
          {
            "imageId": "2",
            "imageName": "germany2",
            "imageBigName": "germany2_BIG",
            "imageRights": "Peter"
          }
        ]
      }
    ]
  }
]

And it should be like this:

[
    {
      "imageSlider": [
        {
          "image": [
            {
              "imageId": "1",
              "imageName": "germany1",
              "imageBigName": "germany1_BIG",
              "imageRights": "Peter"
            },
            {
              "imageId": "2",
              "imageName": "germany2",
              "imageBigName": "germany2_BIG",
              "imageRights": "Peter"
            }
          ]
        }
      ]
    }
  ]

Notice that "germany2" item is inside of the array of image.

let decoder = JSONDecoder()
let jsonData = Data(jsonString.utf8)
do {
  let country = try decoder.decode([CountryModel].self, from: jsonData)
  print(country[0].imageSlider[0].image.count)
} catch {
  print(error.localizedDescription)
}

// Console message
2
Jorge
  • 69
  • 1
  • 6
0

Because your JSON contains multiple Images object with the same key! That is not valid and each one overwrites others (randomly probably). You should turn that json to array of objects:

[{
    "imageSlider" : [{
        "image" : [{
            "imageId" : "1",
            "imageName" : "germany1",
            "imageBigName" : "germany1_BIG",
            "imageRights" : "Peter"
        }]},{
            "image" : [{
            "imageId" : "2",
            "imageName" : "germany2",
            "imageBigName" : "germany2_BIG",
            "imageRights" : "Peter"
        }]}
    }]
}]

Also other keys of the CountryModel should be presented since all of them are non-optional.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278