我想获取github API的存储库数据,这是Json文件,如下所示:
[
{
"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 repo: [RepositoryInfo]
}
struct RepositoryInfo: Codable {
var name: String
var html_url: String
var updated_at: String?
var language: String?
var stargazers_count: Int
}
这是我模型中的获取部分,我想从github API的所有数据中获取一个数组:
struct Repo {
var username: String
mutating func fetchRepoData(completion: @escaping (Result<[RepositoryInfo],Error>) -> Void ) {
let task = URLSession.shared.dataTask(with: prepareRequest()) { (data, response, error) in
if error == nil {
if let response = response as? HTTPURLResponse {
print("statusCode: \(response.statusCode)")
}
do {
let userData = try JSONDecoder().decode(Profile.self, from: data!)
completion(.success(userData.repo))
} catch {
print(error)
}
} else {
completion(.failure(LoginError.networkError))
}
}
task.resume()
}
我收到此错误
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))
有人可以帮我吗?好几个小时,我对这部分都有疑问。