As If Prince
As If Prince

Reputation: 297

How to use Alamofire with Codable protocol to parse data in swift?

I am trying to make a network call and parse JSON from the api as i am new to codable i can't do that. This is the response:

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

And this is my network call with codable structure:

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

Help me i am stuck here. i can't get the values here in "users = userData.Users"

"showResponse" prints this block: ---> 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"}]

Upvotes: 1

Views: 4170

Answers (4)

Jon Shier
Jon Shier

Reputation: 12770

I suggest you create a matching Decodable type using something like quicktype.io to ensure you're matching the JSON exactly. Once you have that type, you can use Alamofire's responseDecodable to decode the type from JSON directly:

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

Upvotes: 1

Tushar Moradiya
Tushar Moradiya

Reputation: 2128

Try following code :

  1. define users array :

    var users : [UsersInfo] = [UsersInfo]()
    
  2. Model :

    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 calling method :

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

Upvotes: 0

if you really need an array of users, then try this:

(and take @Joakim Danielson and @George_E advice)

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

EDIT:

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

Upvotes: 2

George
George

Reputation: 30341

You are only getting back data of a single user, which for you is usersInfo.

Try the following code:

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

If instead your data is actually an array of usersInfo, you can do the following:

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

Upvotes: 0

Related Questions