【问题标题】:How to use Alamofire with Codable protocol to parse data in swift?如何使用 Alamofire 和 Codable 协议快速解析数据?
【发布时间】:2021-10-08 11:52:27
【问题描述】:

我正在尝试进行网络调用并从 api 解析 JSON,因为我是可编码的新手,我不能这样做。这是回应:

[{"login": "mojombo",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://avatars.githubusercontent.com/u/1?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/mojombo",
"html_url": "https://github.com/mojombo",
"followers_url": "https://api.github.com/users/mojombo/followers",
"following_url": "https://api.github.com/users/mojombo/following{/other_user}",
"gists_url": "https://api.github.com/users/mojombo/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mojombo/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mojombo/subscriptions",
"organizations_url": "https://api.github.com/users/mojombo/orgs",
"repos_url": "https://api.github.com/users/mojombo/repos",
"events_url": "https://api.github.com/users/mojombo/events{/privacy}",
"received_events_url": "https://api.github.com/users/mojombo/received_events",
"type": "User",
"site_admin": false},

{"login": "defunkt",
"id": 2,
"node_id": "MDQ6VXNlcjI=",
"avatar_url": "https://avatars.githubusercontent.com/u/2?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/defunkt",
"html_url": "https://github.com/defunkt",
"followers_url": "https://api.github.com/users/defunkt/followers",
"following_url": "https://api.github.com/users/defunkt/following{/other_user}",
"gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
"starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
"organizations_url": "https://api.github.com/users/defunkt/orgs",
"repos_url": "https://api.github.com/users/defunkt/repos",
"events_url": "https://api.github.com/users/defunkt/events{/privacy}",
"received_events_url": "https://api.github.com/users/defunkt/received_events",
"type": "User",
"site_admin": false},

{"login": "pjhyett",
"id": 3,
"node_id": "MDQ6VXNlcjM=",
"avatar_url": "https://avatars.githubusercontent.com/u/3?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/pjhyett",
"html_url": "https://github.com/pjhyett",
"followers_url": "https://api.github.com/users/pjhyett/followers",
"following_url": "https://api.github.com/users/pjhyett/following{/other_user}",
"gists_url": "https://api.github.com/users/pjhyett/gists{/gist_id}",
"starred_url": "https://api.github.com/users/pjhyett/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/pjhyett/subscriptions",
"organizations_url": "https://api.github.com/users/pjhyett/orgs",
"repos_url": "https://api.github.com/users/pjhyett/repos",
"events_url": "https://api.github.com/users/pjhyett/events{/privacy}",
"received_events_url": "https://api.github.com/users/pjhyett/received_events",
"type": "User",
"site_admin": false},

{"login": "wycats",
"id": 4,
"node_id": "MDQ6VXNlcjQ=",
"avatar_url": "https://avatars.githubusercontent.com/u/4?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wycats",
"html_url": "https://github.com/wycats",
"followers_url": "https://api.github.com/users/wycats/followers",
"following_url": "https://api.github.com/users/wycats/following{/other_user}",
"gists_url": "https://api.github.com/users/wycats/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wycats/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wycats/subscriptions",
"organizations_url": "https://api.github.com/users/wycats/orgs",
"repos_url": "https://api.github.com/users/wycats/repos",
"events_url": "https://api.github.com/users/wycats/events{/privacy}",
"received_events_url": "https://api.github.com/users/wycats/received_events",
"type": "User",
"site_admin": false},

{"login": "ezmobius",
"id": 5,
"node_id": "MDQ6VXNlcjU=",
"avatar_url": "https://avatars.githubusercontent.com/u/5?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/ezmobius",
"html_url": "https://github.com/ezmobius",
"followers_url": "https://api.github.com/users/ezmobius/followers",
"following_url": "https://api.github.com/users/ezmobius/following{/other_user}",
"gists_url": "https://api.github.com/users/ezmobius/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ezmobius/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ezmobius/subscriptions",
"organizations_url": "https://api.github.com/users/ezmobius/orgs",
"repos_url": "https://api.github.com/users/ezmobius/repos",
"events_url": "https://api.github.com/users/ezmobius/events{/privacy}",
"received_events_url": "https://api.github.com/users/ezmobius/received_events",
"type": "User",
"site_admin": false},

{"login": "ivey",
"id": 6,
"node_id": "MDQ6VXNlcjY=",
"avatar_url": "https://avatars.githubusercontent.com/u/6?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/ivey",
"html_url": "https://github.com/ivey",
"followers_url": "https://api.github.com/users/ivey/followers",
"following_url": "https://api.github.com/users/ivey/following{/other_user}",
"gists_url": "https://api.github.com/users/ivey/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ivey/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ivey/subscriptions",
"organizations_url": "https://api.github.com/users/ivey/orgs",
"repos_url": "https://api.github.com/users/ivey/repos",
"events_url": "https://api.github.com/users/ivey/events{/privacy}",
"received_events_url": "https://api.github.com/users/ivey/received_events",
"type": "User",
"site_admin": false},

{"login": "evanphx",
"id": 7,
"node_id": "MDQ6VXNlcjc=",
"avatar_url": "https://avatars.githubusercontent.com/u/7?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/evanphx",
"html_url": "https://github.com/evanphx",
"followers_url": "https://api.github.com/users/evanphx/followers",
"following_url": "https://api.github.com/users/evanphx/following{/other_user}",
"gists_url": "https://api.github.com/users/evanphx/gists{/gist_id}",
"starred_url": "https://api.github.com/users/evanphx/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/evanphx/subscriptions",
"organizations_url": "https://api.github.com/users/evanphx/orgs",
"repos_url": "https://api.github.com/users/evanphx/repos",
"events_url": "https://api.github.com/users/evanphx/events{/privacy}",
"received_events_url": "https://api.github.com/users/evanphx/received_events",
"type": "User",
"site_admin": false},

{"login": "vanpelt",
"id": 17,
"node_id": "MDQ6VXNlcjE3",
"avatar_url": "https://avatars.githubusercontent.com/u/17?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/vanpelt",
"html_url": "https://github.com/vanpelt",
"followers_url": "https://api.github.com/users/vanpelt/followers",
"following_url": "https://api.github.com/users/vanpelt/following{/other_user}",
"gists_url": "https://api.github.com/users/vanpelt/gists{/gist_id}",
"starred_url": "https://api.github.com/users/vanpelt/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/vanpelt/subscriptions",
"organizations_url": "https://api.github.com/users/vanpelt/orgs",
"repos_url": "https://api.github.com/users/vanpelt/repos",
"events_url": "https://api.github.com/users/vanpelt/events{/privacy}",
"received_events_url": "https://api.github.com/users/vanpelt/received_events",
"type": "User",
"site_admin": false},

{"login": "wayneeseguin",
"id": 18,
"node_id": "MDQ6VXNlcjE4",
"avatar_url": "https://avatars.githubusercontent.com/u/18?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/wayneeseguin",
"html_url": "https://github.com/wayneeseguin",
"followers_url": "https://api.github.com/users/wayneeseguin/followers",
"following_url": "https://api.github.com/users/wayneeseguin/following{/other_user}",
"gists_url": "https://api.github.com/users/wayneeseguin/gists{/gist_id}",
"starred_url": "https://api.github.com/users/wayneeseguin/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/wayneeseguin/subscriptions",
"organizations_url": "https://api.github.com/users/wayneeseguin/orgs",
"repos_url": "https://api.github.com/users/wayneeseguin/repos",
"events_url": "https://api.github.com/users/wayneeseguin/events{/privacy}",
"received_events_url": "https://api.github.com/users/wayneeseguin/received_events",
"type": "User",
"site_admin": false},

{"login": "brynary",
"id": 19,
"node_id": "MDQ6VXNlcjE5",
"avatar_url": "https://avatars.githubusercontent.com/u/19?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/brynary",
"html_url": "https://github.com/brynary",
"followers_url": "https://api.github.com/users/brynary/followers",
"following_url": "https://api.github.com/users/brynary/following{/other_user}",
"gists_url": "https://api.github.com/users/brynary/gists{/gist_id}",
"starred_url": "https://api.github.com/users/brynary/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/brynary/subscriptions",
"organizations_url": "https://api.github.com/users/brynary/orgs",
"repos_url": "https://api.github.com/users/brynary/repos",
"events_url": "https://api.github.com/users/brynary/events{/privacy}",
"received_events_url": "https://api.github.com/users/brynary/received_events",
"type": "User",
"site_admin": false},

{"login": "kevinclark",
"id": 20,
"node_id": "MDQ6VXNlcjIw",
"avatar_url": "https://avatars.githubusercontent.com/u/20?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/kevinclark",
"html_url": "https://github.com/kevinclark",
"followers_url": "https://api.github.com/users/kevinclark/followers",
"following_url": "https://api.github.com/users/kevinclark/following{/other_user}",
"gists_url": "https://api.github.com/users/kevinclark/gists{/gist_id}",
"starred_url": "https://api.github.com/users/kevinclark/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/kevinclark/subscriptions",
"organizations_url": "https://api.github.com/users/kevinclark/orgs",
"repos_url": "https://api.github.com/users/kevinclark/repos",
"events_url": "https://api.github.com/users/kevinclark/events{/privacy}",
"received_events_url": "https://api.github.com/users/kevinclark/received_events",
"type": "User",
"site_admin": false},

{"login": "technoweenie",
"id": 21,
"node_id": "MDQ6VXNlcjIx",
"avatar_url": "https://avatars.githubusercontent.com/u/21?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/technoweenie",
"html_url": "https://github.com/technoweenie",
"followers_url": "https://api.github.com/users/technoweenie/followers",
"following_url": "https://api.github.com/users/technoweenie/following{/other_user}",
"gists_url": "https://api.github.com/users/technoweenie/gists{/gist_id}",
"starred_url": "https://api.github.com/users/technoweenie/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/technoweenie/subscriptions",
"organizations_url": "https://api.github.com/users/technoweenie/orgs",
"repos_url": "https://api.github.com/users/technoweenie/repos",
"events_url": "https://api.github.com/users/technoweenie/events{/privacy}",
"received_events_url": "https://api.github.com/users/technoweenie/received_events",
"type": "User",
"site_admin": false},

{"login": "macournoyer",
"id": 22,
"node_id": "MDQ6VXNlcjIy",
"avatar_url": "https://avatars.githubusercontent.com/u/22?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/macournoyer",
"html_url": "https://github.com/macournoyer",
"followers_url": "https://api.github.com/users/macournoyer/followers",
"following_url": "https://api.github.com/users/macournoyer/following{/other_user}",
"gists_url": "https://api.github.com/users/macournoyer/gists{/gist_id}",
"starred_url": "https://api.github.com/users/macournoyer/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/macournoyer/subscriptions",
"organizations_url": "https://api.github.com/users/macournoyer/orgs",
"repos_url": "https://api.github.com/users/macournoyer/repos",
"events_url": "https://api.github.com/users/macournoyer/events{/privacy}",
"received_events_url": "https://api.github.com/users/macournoyer/received_events",
"type": "User",
"site_admin": false},

{"login": "takeo",
"id": 23,
"node_id": "MDQ6VXNlcjIz",
"avatar_url": "https://avatars.githubusercontent.com/u/23?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/takeo",
"html_url": "https://github.com/takeo",
"followers_url": "https://api.github.com/users/takeo/followers",
"following_url": "https://api.github.com/users/takeo/following{/other_user}",
"gists_url": "https://api.github.com/users/takeo/gists{/gist_id}",
"starred_url": "https://api.github.com/users/takeo/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/takeo/subscriptions",
"organizations_url": "https://api.github.com/users/takeo/orgs",
"repos_url": "https://api.github.com/users/takeo/repos",
"events_url": "https://api.github.com/users/takeo/events{/privacy}",
"received_events_url": "https://api.github.com/users/takeo/received_events",
"type": "User",
"site_admin": false},

{"login": "caged",
"id": 25,
"node_id": "MDQ6VXNlcjI1",
"avatar_url": "https://avatars.githubusercontent.com/u/25?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/caged",
"html_url": "https://github.com/caged",
"followers_url": "https://api.github.com/users/caged/followers",
"following_url": "https://api.github.com/users/caged/following{/other_user}",
"gists_url": "https://api.github.com/users/caged/gists{/gist_id}",
"starred_url": "https://api.github.com/users/caged/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/caged/subscriptions",
"organizations_url": "https://api.github.com/users/caged/orgs",
"repos_url": "https://api.github.com/users/caged/repos",
"events_url": "https://api.github.com/users/caged/events{/privacy}",
"received_events_url": "https://api.github.com/users/caged/received_events",
"type": "User",
"site_admin": false},

{"login": "topfunky",
"id": 26,
"node_id": "MDQ6VXNlcjI2",
"avatar_url": "https://avatars.githubusercontent.com/u/26?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/topfunky",
"html_url": "https://github.com/topfunky",
"followers_url": "https://api.github.com/users/topfunky/followers",
"following_url": "https://api.github.com/users/topfunky/following{/other_user}",
"gists_url": "https://api.github.com/users/topfunky/gists{/gist_id}",
"starred_url": "https://api.github.com/users/topfunky/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/topfunky/subscriptions",
"organizations_url": "https://api.github.com/users/topfunky/orgs",
"repos_url": "https://api.github.com/users/topfunky/repos",
"events_url": "https://api.github.com/users/topfunky/events{/privacy}",
"received_events_url": "https://api.github.com/users/topfunky/received_events",
"type": "User",
"site_admin": false},

{"login": "anotherjesse",
"id": 27,
"node_id": "MDQ6VXNlcjI3",
"avatar_url": "https://avatars.githubusercontent.com/u/27?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/anotherjesse",
"html_url": "https://github.com/anotherjesse",
"followers_url": "https://api.github.com/users/anotherjesse/followers",
"following_url": "https://api.github.com/users/anotherjesse/following{/other_user}",
"gists_url": "https://api.github.com/users/anotherjesse/gists{/gist_id}",
"starred_url": "https://api.github.com/users/anotherjesse/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/anotherjesse/subscriptions",
"organizations_url": "https://api.github.com/users/anotherjesse/orgs",
"repos_url": "https://api.github.com/users/anotherjesse/repos",
"events_url": "https://api.github.com/users/anotherjesse/events{/privacy}",
"received_events_url": "https://api.github.com/users/anotherjesse/received_events",
"type": "User",
"site_admin": false},

{"login": "roland",
"id": 28,
"node_id": "MDQ6VXNlcjI4",
"avatar_url": "https://avatars.githubusercontent.com/u/28?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/roland",
"html_url": "https://github.com/roland",
"followers_url": "https://api.github.com/users/roland/followers",
"following_url": "https://api.github.com/users/roland/following{/other_user}",
"gists_url": "https://api.github.com/users/roland/gists{/gist_id}",
"starred_url": "https://api.github.com/users/roland/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/roland/subscriptions",
"organizations_url": "https://api.github.com/users/roland/orgs",
"repos_url": "https://api.github.com/users/roland/repos",
"events_url": "https://api.github.com/users/roland/events{/privacy}",
"received_events_url": "https://api.github.com/users/roland/received_events",
"type": "User",
"site_admin": false},

{"login": "lukas",
"id": 29,
"node_id": "MDQ6VXNlcjI5",
"avatar_url": "https://avatars.githubusercontent.com/u/29?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/lukas",
"html_url": "https://github.com/lukas",
"followers_url": "https://api.github.com/users/lukas/followers",
"following_url": "https://api.github.com/users/lukas/following{/other_user}",
"gists_url": "https://api.github.com/users/lukas/gists{/gist_id}",
"starred_url": "https://api.github.com/users/lukas/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/lukas/subscriptions",
"organizations_url": "https://api.github.com/users/lukas/orgs",
"repos_url": "https://api.github.com/users/lukas/repos",
"events_url": "https://api.github.com/users/lukas/events{/privacy}",
"received_events_url": "https://api.github.com/users/lukas/received_events",
"type": "User",
"site_admin": false},

{"login": "fanvsfan",
"id": 30,
"node_id": "MDQ6VXNlcjMw",
"avatar_url": "https://avatars.githubusercontent.com/u/30?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/fanvsfan",
"html_url": "https://github.com/fanvsfan",
"followers_url": "https://api.github.com/users/fanvsfan/followers",
"following_url": "https://api.github.com/users/fanvsfan/following{/other_user}",
"gists_url": "https://api.github.com/users/fanvsfan/gists{/gist_id}",
"starred_url": "https://api.github.com/users/fanvsfan/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/fanvsfan/subscriptions",
"organizations_url": "https://api.github.com/users/fanvsfan/orgs",
"repos_url": "https://api.github.com/users/fanvsfan/repos",
"events_url": "https://api.github.com/users/fanvsfan/events{/privacy}",
"received_events_url": "https://api.github.com/users/fanvsfan/received_events",
"type": "User",
"site_admin": false},

{"login": "tomtt",
"id": 31,
"node_id": "MDQ6VXNlcjMx",
"avatar_url": "https://avatars.githubusercontent.com/u/31?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/tomtt",
"html_url": "https://github.com/tomtt",
"followers_url": "https://api.github.com/users/tomtt/followers",
"following_url": "https://api.github.com/users/tomtt/following{/other_user}",
"gists_url": "https://api.github.com/users/tomtt/gists{/gist_id}",
"starred_url": "https://api.github.com/users/tomtt/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/tomtt/subscriptions",
"organizations_url": "https://api.github.com/users/tomtt/orgs",
"repos_url": "https://api.github.com/users/tomtt/repos",
"events_url": "https://api.github.com/users/tomtt/events{/privacy}",
"received_events_url": "https://api.github.com/users/tomtt/received_events",
"type": "User",
"site_admin": false},

{"login": "railsjitsu",
"id": 32,
"node_id": "MDQ6VXNlcjMy",
"avatar_url": "https://avatars.githubusercontent.com/u/32?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/railsjitsu",
"html_url": "https://github.com/railsjitsu",
"followers_url": "https://api.github.com/users/railsjitsu/followers",
"following_url": "https://api.github.com/users/railsjitsu/following{/other_user}",
"gists_url": "https://api.github.com/users/railsjitsu/gists{/gist_id}",
"starred_url": "https://api.github.com/users/railsjitsu/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/railsjitsu/subscriptions",
"organizations_url": "https://api.github.com/users/railsjitsu/orgs",
"repos_url": "https://api.github.com/users/railsjitsu/repos",
"events_url": "https://api.github.com/users/railsjitsu/events{/privacy}",
"received_events_url": "https://api.github.com/users/railsjitsu/received_events",
"type": "User",
"site_admin": false},

{"login": "nitay",
"id": 34,
"node_id": "MDQ6VXNlcjM0",
"avatar_url": "https://avatars.githubusercontent.com/u/34?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/nitay",
"html_url": "https://github.com/nitay",
"followers_url": "https://api.github.com/users/nitay/followers",
"following_url": "https://api.github.com/users/nitay/following{/other_user}",
"gists_url": "https://api.github.com/users/nitay/gists{/gist_id}",
"starred_url": "https://api.github.com/users/nitay/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/nitay/subscriptions",
"organizations_url": "https://api.github.com/users/nitay/orgs",
"repos_url": "https://api.github.com/users/nitay/repos",
"events_url": "https://api.github.com/users/nitay/events{/privacy}",
"received_events_url": "https://api.github.com/users/nitay/received_events",
"type": "User",
"site_admin": false},

{"login": "kevwil",
"id": 35,
"node_id": "MDQ6VXNlcjM1",
"avatar_url": "https://avatars.githubusercontent.com/u/35?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/kevwil",
"html_url": "https://github.com/kevwil",
"followers_url": "https://api.github.com/users/kevwil/followers",
"following_url": "https://api.github.com/users/kevwil/following{/other_user}",
"gists_url": "https://api.github.com/users/kevwil/gists{/gist_id}",
"starred_url": "https://api.github.com/users/kevwil/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/kevwil/subscriptions",
"organizations_url": "https://api.github.com/users/kevwil/orgs",
"repos_url": "https://api.github.com/users/kevwil/repos",
"events_url": "https://api.github.com/users/kevwil/events{/privacy}",
"received_events_url": "https://api.github.com/users/kevwil/received_events",
"type": "User",
"site_admin": false},

{"login": "KirinDave",
"id": 36,
"node_id": "MDQ6VXNlcjM2",
"avatar_url": "https://avatars.githubusercontent.com/u/36?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/KirinDave",
"html_url": "https://github.com/KirinDave",
"followers_url": "https://api.github.com/users/KirinDave/followers",
"following_url": "https://api.github.com/users/KirinDave/following{/other_user}",
"gists_url": "https://api.github.com/users/KirinDave/gists{/gist_id}",
"starred_url": "https://api.github.com/users/KirinDave/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/KirinDave/subscriptions",
"organizations_url": "https://api.github.com/users/KirinDave/orgs",
"repos_url": "https://api.github.com/users/KirinDave/repos",
"events_url": "https://api.github.com/users/KirinDave/events{/privacy}",
"received_events_url": "https://api.github.com/users/KirinDave/received_events",
"type": "User",
"site_admin": false},

{"login": "jamesgolick",
"id": 37,
"node_id": "MDQ6VXNlcjM3",
"avatar_url": "https://avatars.githubusercontent.com/u/37?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/jamesgolick",
"html_url": "https://github.com/jamesgolick",
"followers_url": "https://api.github.com/users/jamesgolick/followers",
"following_url": "https://api.github.com/users/jamesgolick/following{/other_user}",
"gists_url": "https://api.github.com/users/jamesgolick/gists{/gist_id}",
"starred_url": "https://api.github.com/users/jamesgolick/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/jamesgolick/subscriptions",
"organizations_url": "https://api.github.com/users/jamesgolick/orgs",
"repos_url": "https://api.github.com/users/jamesgolick/repos",
"events_url": "https://api.github.com/users/jamesgolick/events{/privacy}",
"received_events_url": "https://api.github.com/users/jamesgolick/received_events",
"type": "User",
"site_admin": false}]

这是我的具有可编码结构的网络调用:

struct UserInfo: Decodable {
  let Users: [usersInfo]
}

struct usersInfo: Codable {
  let login: String
  let url: String
  let avatar_url: String
}

func alamoFireRequest(requestURL: String) {
    Alamofire.request(requestURL, method: .get).response { [self]
        response in
        guard let data = response.data else { return }
        do {
            let decoder = JSONDecoder()
            let userData = try decoder.decode(UserInfo.self, from: data)
            print(userData)
            users = userData.Users
            print(users)
        } catch let error {
            print(error)
        }
    }
}}

帮帮我,我被困在这里了。我无法在“users = userData.Users”中获取值

"showResponse" 打印这个块:---> response json:

 [{
"id" : 1,
"organizations_url" : "https:\/\/api.github.com\/users\/mojombo\/orgs",
"received_events_url" : "https:\/\/api.github.com\/users\/mojombo\/received_events",
"following_url" : "https:\/\/api.github.com\/users\/mojombo\/following{\/other_user}",
"login" : "mojombo",
"avatar_url" : "https:\/\/avatars.githubusercontent.com\/u\/1?v=4",
"url" : "https:\/\/api.github.com\/users\/mojombo",
"node_id" : "MDQ6VXNlcjE=",
"subscriptions_url" : "https:\/\/api.github.com\/users\/mojombo\/subscriptions",
"repos_url" : "https:\/\/api.github.com\/users\/mojombo\/repos",
"type" : "User",
"html_url" : "https:\/\/github.com\/mojombo",
"events_url" : "https:\/\/api.github.com\/users\/mojombo\/events{\/privacy}",
"site_admin" : false,
"starred_url" : "https:\/\/api.github.com\/users\/mojombo\/starred{\/owner}{\/repo}",
"gists_url" : "https:\/\/api.github.com\/users\/mojombo\/gists{\/gist_id}",
"gravatar_id" : "",
"followers_url" : "https:\/\/api.github.com\/users\/mojombo\/followers"},{
"id" : 2,
"organizations_url" : "https:\/\/api.github.com\/users\/defunkt\/orgs",
"received_events_url" : "https:\/\/api.github.com\/users\/defunkt\/received_events",
"following_url" : "https:\/\/api.github.com\/users\/defunkt\/following{\/other_user}",
"login" : "defunkt",
"avatar_url" : "https:\/\/avatars.githubusercontent.com\/u\/2?v=4",
"url" : "https:\/\/api.github.com\/users\/defunkt",
"node_id" : "MDQ6VXNlcjI=",
"subscriptions_url" : "https:\/\/api.github.com\/users\/defunkt\/subscriptions",
"repos_url" : "https:\/\/api.github.com\/users\/defunkt\/repos",
"type" : "User",
"html_url" : "https:\/\/github.com\/defunkt",
"events_url" : "https:\/\/api.github.com\/users\/defunkt\/events{\/privacy}",
"site_admin" : false,
"starred_url" : "https:\/\/api.github.com\/users\/defunkt\/starred{\/owner}{\/repo}",
"gists_url" : "https:\/\/api.github.com\/users\/defunkt\/gists{\/gist_id}",
"gravatar_id" : "",
"followers_url" : "https:\/\/api.github.com\/users\/defunkt\/followers"}]

【问题讨论】:

  • 您确实应该在问题中包含错误消息,但对我来说,您似乎只能获得 1 个用户,因此请尝试在解码时将 UserInfo.self 更改为 usersInfo.self。并且总是以大写字母开头的结构名称,使用几乎相同的名称但第一个小写字母是一个非常糟糕的习惯,只会导致问题更不用说我们很难帮助您使用这些令人困惑的名称
  • @JoakimDanielson ,感谢您的提示,我需要访问数组下的所有值,该怎么办?
  • 但是你得到一个错误,不是吗?
  • @AsIfPrince 能否请您添加有问题的完整回复?

标签: ios swift alamofire codable


【解决方案1】:

我建议您使用 quicktype.io 之类的东西创建一个匹配的 Decodable 类型,以确保您完全匹配 JSON。获得该类型后,您可以使用 Alamofire 的 responseDecodable 直接从 JSON 解码类型:

AF.request(...).responseDecodable(of: YourType.self) { response in
  // Process the response.
}

【讨论】:

    【解决方案2】:

    试试下面的代码:

    1. 定义用户数组:

      var users : [UsersInfo] = [UsersInfo]()
      
    2. 型号:

      struct UsersInfo : Codable {
      
         let login : String?
         let url : String?
         let avatarUrl : String?
      
         enum CodingKeys: String, CodingKey {
               case login = "login"
               case url = "url"
               case avatarUrl = "avatar_url"
      
         }
      }
      
    3. API调用方式:

      func alamoFireRequest(requestURL: String) {
         AF.request(requestURL, method: .get, encoding: URLEncoding.default ,headers: headers).responseJSON { responseData in 
      
         guard let jsonData = responseData.data else {
               return
         }
      
         let decoder = JSONDecoder()
           do {
                users = try decoder.decode([UsersInfo].self, from: jsonData)
           } catch {
               print("getDataList Unexpected error: \(error).")
           }
       }
      

    【讨论】:

    • JSONSerialization 是多余的。 response.data 是解码器需要的。而且它不关心人类的可读性。
    【解决方案3】:

    如果你真的需要一组用户,那么试试这个:

    (并接受@Joakim Danielson 和@George_E 的建议)

    func alamoFireRequest(requestURL: String) {
        Alamofire.request(requestURL, method: .get).response { [self]
            response in
            guard let data = response.data else { return }
            showResponse(data)  // <--- here
            do {
                let decoder = JSONDecoder()
                let userData = try decoder.decode(usersInfo.self, from: data)
                print(userData)
                // users = [userData]  // <--- here
                users.append(userData) // or <--- here
                print(users)
            } catch let error {
                print(error)
            }
        }
    }}
    

    编辑:

    func showResponse(_ data: Data) {
        if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers), let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) {
            print("\n---> response json: " + String(decoding: jsonData, as: UTF8.self))
        } else {
            print("=========> json data malformed")
        }
    }
    

    【讨论】:

    • var users = [usersInfo](),对吗?
    • 是的,如果这是你声明用户的方式,那么我用这个新信息更新了我的答案。
    • typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "期望解码 Dictionary 但找到了一个数组。 ",基础错误:nil))
    • 您在“alamoFireRequest”的哪个位置收到此错误?
    • typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "期望解码 Dictionary 但找到了一个数组。 ",underlyingError: nil))---- 在 alamofire 的 "print(error)" 中得到这个错误
    【解决方案4】:

    你只取回单个用户的数据,对你来说是usersInfo

    试试下面的代码:

    /* ... */
    let decoder = JSONDecoder()
    let userData = try decoder.decode(usersInfo.self, from: data)
    print(userData)
    print(userData.url)
    

    如果您的数据实际上是 usersInfo 的数组,您可以执行以下操作:

    /* ... */
    let decoder = JSONDecoder()
    let usersData = try decoder.decode([usersInfo].self, from: data)
    print(usersData)
    print(usersData[0].url)
    

    【讨论】:

    • 我需要将数据填充到表格视图中,将数据放在数组中对我很有帮助
    • @AsIfPrince 你得到的 JSON 数据是一个单一的 usersInfo 对象,还是一个 usersInfo 的数组?第二个例子是当你有一个数据数组时,你可以用这个数组来制作你的表格视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多