Access an array within a Json in Swift 3

0

Good afternoon, I'm using the Spotify API, I try to access this Json:

I was able to successfully access the "name" value, but I would like to access the "images" array and its "url" value, so, specifically, my question would be, how to access "images" and get the value "url" "?

{
"artists" : [ {
"external_urls" : {
  "spotify" : "https://open.spotify.com/artist/2CIMQHirSU0MQqyYHq0eOx"
},
"followers" : {
  "href" : null,
  "total" : 1685704
},
"genres" : [ "big room", "brostep", "edm", "electro house", "progressive house" ],
"href" : "https://api.spotify.com/v1/artists/2CIMQHirSU0MQqyYHq0eOx",
"id" : "2CIMQHirSU0MQqyYHq0eOx",
"images" : [ {
  "height" : 640,
  "url" : "https://i.scdn.co/image/86e8991077d6ce237be8f24dbc65e90f2e1f2a43",
  "width" : 640
}, {
  "height" : 320,
  "url" : "https://i.scdn.co/image/cd362662352ce88b693b1a31cf5c9784730291dd",
  "width" : 320
}, {
  "height" : 160,
  "url" : "https://i.scdn.co/image/70e010473dc0f158253111e035c71086fb7904fa",
  "width" : 160
} ],
"name" : "deadmau5",
"popularity" : 71,
"type" : "artist",
"uri" : "spotify:artist:2CIMQHirSU0MQqyYHq0eOx"
 }, {
"external_urls" : {
  "spotify" : "https://open.spotify.com/artist/57dN52uHvrHOxijzpIgu3E"
},
"followers" : {
  "href" : null,
  "total" : 355935
},
"genres" : [ "alternative dance", "brooklyn indie", "electronic", "indie pop", "indietronica", "new rave" ],
"href" : "https://api.spotify.com/v1/artists/57dN52uHvrHOxijzpIgu3E",
"id" : "57dN52uHvrHOxijzpIgu3E",
"images" : [ {
  "height" : 693,
  "url" : "https://i.scdn.co/image/2f0c6c465a83cd196e651e3d4e7625ba799a6f60",
  "width" : 1000
}, {
  "height" : 444,
  "url" : "https://i.scdn.co/image/4e3e13c8b993bde9898e49509fb9ae121636e05f",
  "width" : 640
}, {
  "height" : 139,
  "url" : "https://i.scdn.co/image/dc68dd24b45b74ecce9d4ed486423673d683ced3",
  "width" : 200
}, {
  "height" : 44,
  "url" : "https://i.scdn.co/image/4e55ca05d4f336a2fa0e3062a7ec9778a201e8bc",
  "width" : 63
} ],
"name" : "Ratatat",
"popularity" : 70,
"type" : "artist",
"uri" : "spotify:artist:57dN52uHvrHOxijzpIgu3E"
} ]
}

And this is the code I'm using in iOS Swift 3:

let url = "https://api.spotify.com/v1/artists?ids=6XyY86QOPPrYVGvF9ch6wz,74XFHRwlV6OrjEM0A2NCMF,12Chz98pHFMPJEknJQMWvI,6IdtcAwaNVAggwd6sCKgTI,2af5rOpTISbX2YSVYiqoFJ,0yNSzH5nZmHzeE2xn6Xshb,1GImnM7WYVp95431ypofy9,2F9pvj94b52wGKs0OqiNi2,27Owkm4TGlMqb0BqaEt3PW,7oPftvlwr6VrsViSDV7fJY"

var gruposObtenidos: NSMutableArray = []

@IBOutlet var gruposTabNormal: UICollectionView!



override func viewDidLoad() {
    super.viewDidLoad()


    if WebServiceController.conexionInternet(){

        WebServiceController.sharedInstance.callUrlWithCompletion(url: url, params: nil, completion: { (finished, response) in

            if finished{

                //print(response)

                //Para nombre de los grupos
                let resultadoJson = NSMutableArray(array: response["artists"] as! NSArray)


                self.gruposObtenidos = resultadoJson


                self.gruposTabNormal.reloadData()

            }else{

                print("Sin conexión con el servidor")

            }


        }, method: .get)


    }else{

        print("No hay internet")

    }


}

This is my controller of the UICollectionView cell:

class CancionesNormalesCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imgGrupo: UIImageView!

@IBOutlet weak var txtNombreGrupo: UILabel!

var grupos: Dictionary<String, AnyObject>!    


func iniciarCelda(){

    let nombreGrupo = self.grupos["name"] as! String
    self.txtNombreGrupo.text = nombreGrupo

 }
}
    
asked by Jorge Requez 14.06.2017 в 20:54
source

2 answers

1

Following the way you have taken the name, this would be my solution to remove all the urls from the images

func iniciarCelda(){

    let nombreGrupo = self.grupos["name"] as! String
    self.txtNombreGrupo.text = nombreGrupo
    let imagenesObjeto = grupos["images"] as! [[String : AnyObject]]
    var imagenesUrl = [String]()
    for i in imagenesObjeto{
        imagenesUrl.append(i["url"]! as! String)
    }
}
    
answered by 15.06.2017 / 08:36
source
1

Look at this tutorial, I think it can help you.

link

    
answered by 15.06.2017 в 08:12