Как использовать Alamofire с кодируемым протоколом для анализа данных в swift?

#ios #swift #alamofire #codable

Вопрос:

Я пытаюсь сделать сетевой вызов и проанализировать JSON из api, так как я новичок в кодировании, я не могу этого сделать. Это и есть ответ:

 [{"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)
        }
    }
}}
 

Помоги мне, я застрял здесь. я не могу получить значения здесь, в «пользователи = данные пользователя.Пользователи»

«showResponse» выводит этот блок: — — — > ответ 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. Вам действительно следует включить сообщение об ошибке в свой вопрос, но мне кажется, что вы возвращаете только 1 пользователя, поэтому попробуйте переключиться UserInfo.self на usersInfo.self при декодировании. И всегда начинайте имена структур с прописной буквы, используя почти одно и то же имя, но с первой строчной буквы-это очень плохая привычка, которая приведет только к проблемам, не говоря уже о том, что нам сложнее помочь вам с такими запутанными именами

2. @JoakimDanielson ,Спасибо за ваш совет, мне нужно получить доступ ко всем значениям в массиве, что делать?

3. Но вы получаете ошибку, не так ли?

4. @AsIfPrince Не могли бы вы, пожалуйста, добавить полный ответ на вопрос?

Ответ №1:

если вам действительно нужен массив пользователей, попробуйте это:

(и примите советы @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")
    }
}
 

Комментарии:

1. var пользователи = [usersInfo](), это правильно?

2. да, если вы так объявляете пользователей, то я обновил свой ответ этой новой информацией.

3. Сопоставление типов(Свифт. Словарь Строка, Любая>, Быстрая. Ошибка декодирования. Контекст(Путь к кодированию: [], Описание отладки: «Ожидается декодирование словаря<Строка, Любая>, но вместо этого найден массив.», Ошибка в основе: ноль))

4. где в «alamoFireRequest» вы получаете эту ошибку?

5. Сопоставление типов(Свифт. Словарь Строка, Любая>, Быстрая. Ошибка декодирования. Контекст(Путь к кодированию: [], Описание отладки: «Ожидается декодирование словаря<Строка, любая>, но вместо этого найден массив.», Ошибка в основе: ноль))—- получил эту ошибку в «печать(ошибка)» в alamofire

Ответ №2:

Вы получаете только данные одного пользователя, что для вас так и есть 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)
 

Комментарии:

1. мне нужно заполнить данные в табличном представлении, мне было бы полезно иметь данные в массиве

2. @AsIfPrince-это данные JSON, которые вы получаете только один usersInfo объект или массив usersInfo ? 2 — й пример-это когда у вас есть массив данных, и вы можете создать свой tableview с помощью этого массива.

Ответ №3:

Попробуйте выполнить следующий код :

  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).")
         }
     }
     

Комментарии:

1. JSONSerialization это излишне. response.data это то, что нужно декодеру. И его не волнует читабельность для человека.

Ответ №4:

Я предлагаю вам создать соответствующий Decodable тип, используя что-то вроде quicktype.ввод-вывод, чтобы убедиться, что вы точно соответствуете JSON. Как только у вас есть этот тип, вы можете использовать Alamofire responseDecodable для прямого декодирования типа из JSON:

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