Arrays 当字典的值是字符串数组时使用可解码的解析
我想解析下面的json文件,但我一直遇到以下错误: 类型不匹配(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“应解码数组,但找到了字典。”,underlineerror:nil)) 我看过stackoverflow,但它们似乎没有帮助,因为我没有看到其他json文件返回字符串数组的字典值。有什么想法吗Arrays 当字典的值是字符串数组时使用可解码的解析,arrays,json,swift,decodable,Arrays,Json,Swift,Decodable,我想解析下面的json文件,但我一直遇到以下错误: 类型不匹配(Swift.Array,Swift.DecodingError.Context(codingPath:[],debugDescription:“应解码数组,但找到了字典。”,underlineerror:nil)) 我看过stackoverflow,但它们似乎没有帮助,因为我没有看到其他json文件返回字符串数组的字典值。有什么想法吗 //JSON file { pugs: [ "http://27.media.tumblr
//JSON file
{
pugs: [
"http://27.media.tumblr.com/tumblr_lqfwfyZuyS1qiyqyfo1_500.jpg",
"http://30.media.tumblr.com/tumblr_liic7bdmRF1qcipjro1_500.jpg",
"http://29.media.tumblr.com/tumblr_ll3xdp73DQ1qb08qmo1_500.jpg",
"http://29.media.tumblr.com/tumblr_lht5uy6khS1qed3e3o1_500.jpg",
"http://26.media.tumblr.com/tumblr_lrqnevtBvM1qb08qmo1_400.jpg",
"http://26.media.tumblr.com/tumblr_ll90kwmMJw1qzj3syo1_500.jpg",
"http://26.media.tumblr.com/tumblr_ll7aoxHGfW1qb08qmo1_500.jpg",
"http://24.media.tumblr.com/tumblr_lk27smb4sR1qzj3syo1_500.jpg",
"http://26.media.tumblr.com/tumblr_lil8a1m1YM1qzj3syo1_500.jpg",
"http://27.media.tumblr.com/tumblr_liy1xfY9G71qftdfxo1_500.jpg"
]
}
您不需要
Pug
类。将pugs
更改为[String]
顺便说一句,除非你有充分的理由使用类,否则使用
struct
而不是class
。BTW,你展示的json是无效的。。。您可能错过了]
@RobertDresler谢谢您的关注。我没有发布整个阵列,缺少结束括号。。。。甚至到[URL]
@mir也很乐意帮忙。请不要忘记单击答案左侧的复选标记,表明您的问题已解决。
//Data Model
class PugList: Codable {
var pugs: [Pug]
var likes: Int
}
class Pug: Codable {
var images: [String]
}