Swift Jsondecoder typeMismatch error

Asked

Viewed 95 times

0

I’m trying to use Jsondecoder to convert a JSON to structs in Swift, so I wrote down all the Structs, reviewed it for hours, and it still returns this error. I don’t know if there’s any way of knowing exactly which struct is causing trouble. I will post the code and links to two JSON files, one that is working well, and the other that is giving this problem.

The error returned is:

typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to Decode Dictionary but found an array Instead." , underlyingError: nil))

// Created by Breno Ramos on 12/28/17. // Copyright 2017 brenor2. All Rights reserved. //

import Foundation

struct Owner:Decodable { Let login : String? Let id : Double? Let avatar_url : String? Let gravatar_id : String? Let url : String? Let html_url : String? Let followers_url : String? Let following_url : String? Let gists_url : String? Let starred_url : String? Let subscriptions_url : String? Let organizations_url : String? Let repos_url : String? Let events_url : String? Let received_events_url : String? Let type : String? Let site_admin : Bool? }

struct License:Decodable { Let key : String? Let name : String? Let spdx_id : String? Let url : String? }

struct Repo:Decodable { Let id : Double? Let name : String? Let full_name : String? Let Owner : Owner? Let private : Bool? Let html_url : String? Let Description : String? Let Fork : Bool? Let url : String? Let forks_url : String? Let keys_url : String? Let collaborators_url : String? Let teams_url : String? Let hooks_url : String? Let issue_events_url : String? Let events_url : String? Let assignees_url : String? Let branches_url : String? Let tags_url : String? Let blobs_url : String? Let git_tags_url : String? Let git_refs_url : String? Let trees_url : String? Let statuses_url : String? Let languages_url : String? Let stargazers_url : String? Let contributors_url : String? Let subscribers_url : String? Let subscription_url : String? Let commits_url : String? Let git_commits_url : String? Let comments_url : String? Let issue_comment_url : String? Let contents_url : String? Let compare_url : String? Let merges_url : String? Let archive_url : String? Let downloads_url : String? Let issues_url : String? Let pulls_url : String? Let milestones_url : String? Let notifications_url : String? Let labels_url : String? Let releases_url : String? Let deployments_url : String? Let created_at : String? Let updated_at : String? Let pushed_at : String? Let git_url : String? Let ssh_url : String? Let clone_url : String? Let svn_url : String? Let homepage : String? Let size : Double? Let stargazers_count : Double? Let watchers_count : Double? Let language : String? Let has_issues : Bool? Let has_projects : Bool? Let has_downloads : Bool? Let has_wiki : Bool? Let has_pages : Bool? Let forks_count : Double? Let mirror_url : String? Let archived : Bool? Let open_issues_count : Double? Let License : License? Let Forks : Double? Let open_issues : Double? Let Topics : Topic? Let Permissions : Permissions? Let Watchers : Double? Let default_branch : String? // Let score : Double? // Let subscribers_count : Double? // Let network_count : Double? // Let allow_rebase_merge: Bool? // Let allow_squash_merge: Bool? // Let allow_merge_commit: Bool?

}

struct Topic:Decodable { Let Topics : [String]? }

struct Permissions:Decodable { Let admin : Bool Let push : Bool Let pull : Bool }

struct Repolist:Decodable{ Let total_count : Int? Let incomplete_results : Bool? Let items : [Repo]? }

struct User:Decodable { Let login: String? Let id: Double? Let avatar_url: String? Let gravatar_id: String? Let url: String? Let html_url: String? Let followers_url: String? Let following_url: String? Let gists_url: String? Let starred_url: String? Let subscriptions_url: String? Let organizations_url: String? Let repos_url: String? Let events_url: String? Let received_events_url: String? Let type: String? Let site_admin: Bool? }

struct Creator:Decodable { Let login: String? Let id: Double? Let avatar_url: String? Let gravatar_id: String? Let url: String? Let html_url: String? Let followers_url: String? Let following_url: String? Let gists_url: String? Let starred_url: String? Let subscriptions_url: String? Let organizations_url: String? Let repos_url: String? Let events_url: String? Let received_events_url: String? Let type: String? Let site_admin: Bool? }

struct Link:Decodable { Let href :String? }

struct _Links:Decodable { Let self :Link? Let html :Link? Let Issue :Link? Let comments :Link? Let review_comments :Link? Let review_comment :Link? Let commits :Link? Let statuses :Link? }

struct Base:Decodable { Let label :String? Let ref :String? Let sha :String? Let user :User? Let Repo :Repo? }

struct Head:Decodable { Let label :String? Let ref :String? Let sha :String? Let user :User? Let Repo :Repo? }

struct Milestone:Decodable { Let url:String? Let html_url:String? Let labels_url:String? Let id: Double? Let number:Double? Let title:String? Let Description:String? Let Creator:Creator? Let open_issues:Double? Let closed_issues:Double? Let state:String? Let created_at:String? Let updated_at:String? Let closed_at:String? Let due_on:String? }

struct Assignee:Decodable { Let login :String? Let id :Double? Let avatar_url :String? Let gravatar_id :String? Let url :String? Let html_url :String? Let followers_url :String? Let following_url :String? Let gists_url :String? Let starred_url :String? Let subscriptions_url :String? Let organizations_url :String? Let repos_url :String? Let events_url :String? Let received_events_url :String? Let type :String? Let site_admin :Bool? }

struct Reviewers:Decodable { Let login: String? Let id: Double? Let avatar_url: String? Let gravatar_id: String? Let url: String? Let html_url: String? Let followers_url: String? Let following_url: String? Let gists_url: String? Let starred_url: String? Let subscriptions_url: String? Let organizations_url: String? Let repos_url: String? Let events_url: String? Let received_events_url: String? Let type: String? Let site_admin: Bool? }

struct Pull:Decodable { Let id: Double? Let url:String? Let html_url:String? Let diff_url:String? Let patch_url:String? Let issue_url:String? Let number:Double? Let state:String? Let locked:Bool? Let title:String? Let user:User? Let body:String? Let created_at:String? Let updated_at:String? Let closed_at:String? Let merged_at:String? Let merge_commit_sha: String? Let assignee: Assignee? Let assignees: [Assignee]? Let requested_reviewers: [Reviewers]? Let Milestone:Milestone? Let commits_url:String? Let review_comments_url:String? Let review_comment_url:String? Let comments_url:String? Let statuses_url:String? Let head:head? Let base:Base? Let _links:_Links? Let author_association:String? }

struct Pulllist:Decodable { Let pulls:[Pull]? }

/////////////////////////////////////////////////////////

1.This is working well with these structures 2.This is what returns the error

  • Here it is in English. Translate your question.

  • Thousand apologies, translated. Thank you for the touch!

1 answer

0


The error is happening because on the first link you are receiving a JSON object and on the second a JSON Array: Note that on the first link the JSON starts with { this means that it is an object already on the second it starts with [ this means that it is an array. In this case convert what you receive in link 2 to an array and then try to give the Decode.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.