Ios Json解码器Swift中的类型不匹配
我想获取github API的存储库数据,以下是Json文件:Ios Json解码器Swift中的类型不匹配,ios,json,swift,jsondecoder,Ios,Json,Swift,Jsondecoder,我想获取github API的存储库数据,以下是Json文件: [ { "id": 166694221, "node_id": "MDEwOlJlcG9zaXRvcnkxNjY2OTQyMjE=", "name": "AmA", "full_name": "mxcl/AmA", "private": false, "owner": { "login": "mxcl", "id": 58962,
[
{
"id": 166694221,
"node_id": "MDEwOlJlcG9zaXRvcnkxNjY2OTQyMjE=",
"name": "AmA",
"full_name": "mxcl/AmA",
"private": false,
"owner": {
"login": "mxcl",
"id": 58962,
"node_id": "MDQ6VXNlcjU4OTYy",
"avatar_url": "https://avatars2.githubusercontent.com/u/58962?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/mxcl",
"html_url": "https://github.com/mxcl",
},
"html_url": "https://github.com/mxcl/AmA",
"description": "Ask mxcl anything.",
"fork": false,
"url": "https://api.github.com/repos/mxcl/AmA",
"languages_url": "https://api.github.com/repos/mxcl/AmA/languages",
"merges_url": "https://api.github.com/repos/mxcl/AmA/merges",
"downloads_url": "https://api.github.com/repos/mxcl/AmA/downloads",
"created_at": "2019-01-20T18:22:04Z",
"updated_at": "2019-08-29T21:03:39Z",
"pushed_at": "2019-03-12T13:35:23Z",
"size": 3,
"stargazers_count": 23,
"watchers_count": 23,
"language": null,
"default_branch": "master"
},
{
"id": 170758212,
"node_id": "MDEwOlJlcG9zaXRvcnkxNzA3NTgyMTI=",
"name": "AppUpdater",
"full_name": "mxcl/AppUpdater",
"private": false,
"owner": {
"login": "mxcl",
"id": 58962,
"node_id": "MDQ6VXNlcjU4OTYy",
"avatar_url": "https://avatars2.githubusercontent.com/u/58962?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/mxcl",
"html_url": "https://github.com/mxcl",
"followers_url": "https://api.github.com/users/mxcl/followers",
"following_url": "https://api.github.com/users/mxcl/following{/other_user}",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/mxcl/AppUpdater",
"description": "Automatically update open source macOS apps from GitHub releases.",
"fork": false,
"url": "https://api.github.com/repos/mxcl/AppUpdater",
"languages_url": "https://api.github.com/repos/mxcl/AppUpdater/languages",
"created_at": "2019-02-14T21:07:26Z",
"updated_at": "2019-08-31T21:47:34Z",
"size": 27,
"stargazers_count": 202,
"watchers_count": 202,
"language": "Swift",
"default_branch": "master"
}
]
以下是我的回复结构:
struct Profile:Codable{
var回购:[回购信息]
}
结构库信息:可编码{
变量名称:String
var html_url:String
var更新位置:String?
变量语言:字符串?
var stargazers_计数:Int
}
这是我的模型中的获取部分,我想从github API的所有数据中获取一个数组:
struct Repo{
var用户名:String
变异func fetchRepoData(完成:@escaping(Result)->Void){
让task=URLSession.shared.dataTask(带:prepareRequest()){(数据、响应、错误)在
如果错误==nil{
如果let response=response as?HTTPURLResponse{
打印(“状态代码:\(response.statusCode)”)
}
做{
让userData=try JSONDecoder().decode(Profile.self,from:data!)
完成(.success(userData.repo))
}抓住{
打印(错误)
}
}否则{
完成(.failure(LoginError.networkError))
}
}
task.resume()
}
我得到了这个错误
类型不匹配(Swift.Dictionary,Swift.DecodingError.Context(codingPath:[],debugDescription:“应解码字典,但找到了一个数组。”,underlineerror:nil))
有人能帮我吗?好几个小时以来,我对这部分都有问题。似乎json是
[RepositoryInfo].self
不是Profile.self
那么我应该怎么改变这一行,让userData=try JSONDecoder().decode(Profile.self,from:data!)
如上所述。让userData=try JSONDecoder().decode([RepositoryInfo].self,from:data!)
仅从快速的第一眼看,JSON是否属于[[String:Any]]
类型,而不仅仅是[String:Any]
?也就是说,JSON是字符串:Any
字典的数组,而不仅仅是字符串:Any
字典本身。