How to parse a JSON with array of dictionaries within another array with Swift and alamofire

0

I am trying to walk an object with alamofire, and everything is fine until I have to deal with the array of dictionaries that I have within that same object. I think I have a problem with the model, but I'm not sure and since I did not find any documents I decided to ask.

Model class:

import UIKit
import RealmSwift
import ObjectMapper

class Constelation : Mappable {

\ aqui el typalias lo creo para intentar parsear el array de diccionario

typealias dictionary = [[String : String]]

  var itemId : String?
  var name : String?
  var subtitle : String?
  var body : String?
  var imagen : String?
  var poster : String?

\ aqui stars me da un error diciéndome que no conforma el protocolo RealmCollectionValue

  var stars = List<Stars>()
  var objects : Array<dictionary>?

  required init?(map: Map) {

  }

  func mapping(map: Map) {
      itemId <- map["itemId"]
      name <- map["name"]
      subtitle <- map["subtitle"]
      body <- map["body"]
      imagen <- map["imagen"]
      poster <- map["poster"]
      stars <- map["stars"]
      objects <- map["objects"]
  }
}

  class Stars: Constelation {

  @objc dynamic var nameStarst: String?


  required convenience init?() {
      self.init()
  }
}

The ViewController class:

import UIKit
import Alamofire
import AlamofireImage
import AlamofireObjectMapper

class ViewController: UITableViewController {

    var constellations: [Constelation]?
    var stars: [Stars]?
    @IBOutlet var table: UITableView!

    override func viewDidLoad() {
      super.viewDidLoad()
      self.setupUI()
      self.setupData()
   }

   func setupUI() {
      self.table.delegate = self
      self.table.dataSource = self
   }

   func setupData() {
      APIController.requestConstelations { (constellationArray, error) in
            print("Terminé de sacar url \(constellationArray?.first?.imagen)")
           print("saco el item \(constellationArray?.first?.itemId)")
           if error != nil {
              print("error")
           }
           else {
                print()
              for x in Stars {
                     print(x)
                  }
           }
       }
    }

}

When I try to print the array it gives me nil or the value of the reference in memory. Is the question whether I have to create a property in the data model or is it using the constellation class and some protocol?

Thanks in advance.

    
asked by Jonay B. 25.10.2018 в 21:29
source

1 answer

1

I do not fully understand the structure of the json that you are trying to parse, however I enclose a possible answer.

If we have a json like the following:

{
 "itemId":"1",
 "name":"Estrella1",
 ...
 "objects":[  
           {  
            "subItem":"2",
            "subName":"SubItem"
           },
           ...
           ]
}

Our models with ObjectMapper would be:

class Constelation : Mappable {
   var itemId : String = ""
   var name : String = ""
   ...
   var objects : Array<Star>?

   init() {}
   required init?(map: Map) {}

   func mapping(map: Map) {
      itemId <- map["itemId"]
      name <- map["name"]
      ...
      objects <- map["objects"]
   }
}

class Star : Mappable {
   var subItem : String = ""
   var subName : String = ""

   init() {}
   required init?(map: Map) { }

   func mapping(map: Map) {
     subItem <- map["subItem"]
     subName <- map["subName"]
   }
}

And on the WebService side it would be

let responseService = Mapper<Constelation>().map(JSON: json)
print(responseService?.objects?[0].subItem)

In the case that your json is an array of array on the WebService side, the instruction would be:

let responseService = Mapper<Constelation>().mapArray(JSONObject: json)
print(responseService?[0].objects?[0].subItem)

Greetings I hope I help you

    
answered by 29.10.2018 / 19:28
source