STRUCT CUSTOM JSON ARRAY?

1

My JSON is this:

{
…
    "episodes": {
        "1": [
            {
                "id": 533072,
                "season": 1,
                "episode": 1,
                "nombre": "La hija del general",
                "firstaired": "2012-12-06",
                "const": 8
            },
            {
                "id": 533078,
                "season": 1,
                "episode": 2,
                "nombre": "Caballo de Troya",
                "firstaired": "2012-12-06",
                "const": 8
            },
            {
                "id": 533073,
                "season": 1,
                "episode": 3,
                "nombre": "Cosecha",
                "firstaired": "2012-12-06",
                "const": 8
            },
…
}

My question is, how do I create the JSON structure in Swift 4? I tried this:

 struct detailInfo: Decodable{
      let seasons: [seasons]?
      let episodes: episodes

}
 struct seasons: Decodable{
      let id: Int
      let season: Int
}
 struct episodes: Decodable{
 ???
}

I do not know how to continue the JSON from here ...

Thanks !!

    
asked by Angel Socias 03.09.2018 в 20:45
source

1 answer

0

I would do it in the following way, maybe you need to structure each parameter as you are implementing it but I would simplify it in a single structure ... in case you only need the "episode" just add the parameters that you are going to use in that struct of "episodes" by the way, I do not know in which way you have your request to get the data from the json ... but even if you are already receiving them with a getRequest only pull them to the structure.

    struct Episodes {
        id: Int
        season: Int
        episode: Int
        nombre: String
        firstaired: Date
        const: Int
        }

then to remove it from the json creates an empty array in the view controller where you will need it ...

         var episodes = [Episodes]()

and when you are in the request to get the json data these usually always come with a completion, but I do not know how you have it, I'll put you for example ...

 func getJsonObject() {
    requestJson { [weak self] (json, error) {  <-
         // no olvides sacar el error con un guard o un if let....
    guard let jsonValue = json.value(forkey: "episodes") as NSDictionary,
    let dictionaryValue = jsonValue.value(forkey: "1" as? [NSDictionary]  else { return }
    for (index, value) in dictionaryValue.enumarate() {
            let id = value.value(forkey: "id") as? Int
            let season = value.value(forkey: "season") as? Int
       }

and so on each one, and to add them to the array that you created because you simply give the set inside the for ....

       let addEpisode = Episodes(id: id, season: season, nombre... y cada uno de los parametros

then to add them to the array you simply give an append ....

        episodes.append(addEpisode)

will go through the elements and will be added in the array ... this is natively .. if you use alamofire or swiftyJSON is much simpler .. another thing it is recommended to put a removeAll of the variable array before the cycle (I imagine you will use a collection view or a TableView) this will prevent the elements from duplicating ...

     episodes.removeAll()
     for (index, value) in. etc etc 

I do not know if it's more or less what you're looking for .. but just try, greetings and I hope I can help you out

    
answered by 05.09.2018 / 05:47
source