Jacobo
Jacobo

Reputation: 1391

Parse and array of objects in JSON to an Object in Swift

Hey I have the following JSON:

{
    "monuments": [
        {
            "name": "Iglesia de Tulyehualco",
            "description": "No hay descripción",
            "latitude": 19.2544877,
            "longitude": -99.012157
        },
        {
            "name": "Casa de Chuyin",
            "description": "Casa de Jesús",
            "latitude": 119.2563629,
            "longitude": -99.0152632
        }
    ]
}

I get the following code to try parse each object but I'm getting the error that type Any has no member 'x'.

func loadMonuments() {
    if let path = Bundle.main.path(forResource: "monuments", ofType: "json") {
        do {
            let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
            let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
            if let jsonResult = jsonResult as? Dictionary<String, AnyObject>, let monumentsJson = jsonResult["monuments"] as? [Any] {
                for m in monumentsJson {
                    print(m)
                }
            }
        } catch {
            // handle error
        }
    }
}

I want to get each property of the monument.

Upvotes: 0

Views: 52

Answers (2)

Felipe Rolvar
Felipe Rolvar

Reputation: 101

In Swift4 was introduced Codable for serialization, so you must try to make your objects Codable like this:

struct Monument: Codable {
  let name: String
  let description: String
  let latitude: String
  let longitude: String
}

And then you can parse the object using this:

func loadMonuments() -> [Monument] {
    guard let path = Bundle.main.path(forResource: "monuments", ofType: "json"),
       let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)  else {
        return []
    }

    do {
        return try JSONDecoder().decode([Monument].self, from: data)
    } catch {
        print("error: \(error)")
        return []
    }
}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

Option1:(recommended)

struct Root:Decodable { 
 let monuments:[InnerItem] 
}

struct InnerItem:Decodable { 
 let name:String
 let description:String
 let latitude:Doube
 let longitude:Double 
}

//

 do {
      let data = try Data(contentsOf: URL(fileURLWithPath: path), options:[])
      let content = try JSONDecoder().decode(Root.self,from:data)
      print(content)
 }
 catch {
  print(error)
 }

Option2:

if let jsonResult = jsonResult as? [String:Any] , let monumentsJson = jsonResult["monuments"] as? [[String:Any]] {
     for m in monumentsJson {
         print(m["name"])
     }
}

Upvotes: 2

Related Questions