简体   繁体   中英

How to parse this json with alamofire

How to parse this json in my code? (Как распарсить этот json в моем коде?). What data model to collect? (какую модель данных собирать?). I don't understand how to cast dictionaries in dictionaries later. (Не пойму как потом кастить словари в словарях).

I get an error opposite let artist:

Value of type 'Dictionary.Element' (aka '(key: String, value: Dictionary)') has no subscripts

func fetchCurrentChartsWithAlamofire(apiMethod: String) {

        let url = "https://"

        request(url).validate().responseJSON { responseData in

            switch responseData.result {
            case .success(let value):

                guard let jsonData = value as? [String:[String:AnyObject]] else { return }

                for artists in jsonData {
                    let artist = Artist(name: artists["artists"])
                }


            case .failure(let error):
                print(error)
            }
        }
    }

Here is json in the browser:

{
"artists": {

"artist": [

{
"name": "The Weeknd",

}
]
}
}

Here is how you can parse this

struct Artist:Decodable {
  let artists:Artists
}
struct Artists:Decodable {
  let artist: [ArtistName]
}

struct ArtistName:Decodable {
  let name: String
}

For json

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM