简体   繁体   中英

errors while parsing json in swift

I want to parse a json but there is a keyword "media-metadata" present. I tried with below codes but getting error "Type 'Media' does not conform to protocol 'Decodable'". Any help how to figure out?

struct TopStoriesResponse: Codable {
    let status:String
    let results:[Result]
}

struct Result: Codable {
    let title: String
    let abstract: String
    let media: [Media]
}
struct Media: Codable {           //Type 'Media' does not conform to protocol 'Decodable'
    let copyright: String
    let mediaMetadata : [MediaMetadataDetails]
    
    enum CodingKeys: String, CodingKey {
       case mediaMetadata = "media-metadata"
    }
}

struct MediaMetadataDetails: Codable {
    let format: String
    let url: String
    
}

Add copyright to CodingKeys

struct Media: Codable {            
   let copyright: String
   let mediaMetadata : [MediaMetadataDetails]

   enum CodingKeys: String, CodingKey {
      case copyright,mediaMetadata = "media-metadata"
   }
}

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