0

I want to parse the json file below, but I keep getting the following error:

typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))

I have looked on stackoverflow, but they didn't seem to help as I don't see other json files that returns a dictionary value that is an array of strings. Any ideas?

//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"
  ]
}
//Data Model
class PugList: Codable {
    var pugs: [Pug]
    var likes: Int
}

class Pug: Codable {
    var images: [String]
}
2
  • 1
    Btw, json which you've showed isn't valid... you're probably missing ] Commented May 27, 2019 at 18:33
  • @RobertDresler thanks for catching that. I didn't post the entire array and was missing the closing bracket. Commented May 27, 2019 at 18:40

1 Answer 1

1

You don't need the Pug class. Change pugs to [String].

BTW - use struct instead of class unless you have a good reason to use a class.

Sign up to request clarification or add additional context in comments.

2 Comments

... or even to [URL] 😉
@mir Glad to help. Please don't forget to indicate that your question has been resolved by clicking the checkmark to the left of the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.