如何用alamofire解析这个json

6tr1vspr  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(143)

如何在我的代码中解析这个JSON?要收集什么数据模型?.我不明白以后如何在字典中强制转换字典。
我得到一个错误对面让艺术家:
类型'Dictionary<String,[String:AnyObject]>.Element'(aka '(key:字符串,值:Dictionary<String,AnyObject>)')没有下标

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)
            }
        }
    }

下面是浏览器中的JSON:

{
"artists": {

"artist": [

{
"name": "The Weeknd",

}
]
}
}
d6kp6zgx

d6kp6zgx1#

下面是如何解析它

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

struct ArtistName:Decodable {
  let name: String
}

对于json

相关问题