如何迭代通過JSON對象來獲取JSON數組在android 我的代碼不返回我的JSON數組中的值,我認爲這是因爲它不是我的JSON對象中的第一項原因大多數我使用的JSON數組通常是他們的JSON對象中的第一個項目......當我替換JSON對象時,代碼完美工作......我只需要知道如何獲得「項目」...請迭代通過JSON對象android
JSON: {
"total_count": 9,
"incomplete_results": false,
"items": [
{
"login": "unicodeveloper",
"id": 2946769,
"avatar_url": "https://avatars2.githubusercontent.com/u/2946769?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/unicodeveloper",
"html_url": "https://github.com/unicodeveloper",
"followers_url": "https://api.github.com/users/unicodeveloper/followers",
"following_url": "https://api.github.com/users/unicodeveloper/following{/other_user}",
"gists_url": "https://api.github.com/users/unicodeveloper/gists{/gist_id}",
"starred_url": "https://api.github.com/users/unicodeveloper/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/unicodeveloper/subscriptions",
"organizations_url": "https://api.github.com/users/unicodeveloper/orgs",
"repos_url": "https://api.github.com/users/unicodeveloper/repos",
"events_url": "https://api.github.com/users/unicodeveloper/events{/privacy}",
"received_events_url": "https://api.github.com/users/unicodeveloper/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "ireade",
"id": 8677283,
"avatar_url": "https://avatars2.githubusercontent.com/u/8677283?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/ireade",
"html_url": "https://github.com/ireade",
"followers_url": "https://api.github.com/users/ireade/followers",
"following_url": "https://api.github.com/users/ireade/following{/other_user}",
"gists_url": "https://api.github.com/users/ireade/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ireade/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ireade/subscriptions",
"organizations_url": "https://api.github.com/users/ireade/orgs",
"repos_url": "https://api.github.com/users/ireade/repos",
"events_url": "https://api.github.com/users/ireade/events{/privacy}",
"received_events_url": "https://api.github.com/users/ireade/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "christiannwamba",
"id": 8108337,
"avatar_url": "https://avatars1.githubusercontent.com/u/8108337?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/christiannwamba",
"html_url": "https://github.com/christiannwamba",
"followers_url": "https://api.github.com/users/christiannwamba/followers",
"following_url": "https://api.github.com/users/christiannwamba/following{/other_user}",
"gists_url": "https://api.github.com/users/christiannwamba/gists{/gist_id}",
"starred_url": "https://api.github.com/users/christiannwamba/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/christiannwamba/subscriptions",
"organizations_url": "https://api.github.com/users/christiannwamba/orgs",
"repos_url": "https://api.github.com/users/christiannwamba/repos",
"events_url": "https://api.github.com/users/christiannwamba/events{/privacy}",
"received_events_url": "https://api.github.com/users/christiannwamba/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "ibrahimlawal",
"id": 458428,
"avatar_url": "https://avatars1.githubusercontent.com/u/458428?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/ibrahimlawal",
"html_url": "https://github.com/ibrahimlawal",
"followers_url": "https://api.github.com/users/ibrahimlawal/followers",
"following_url": "https://api.github.com/users/ibrahimlawal/following{/other_user}",
"gists_url": "https://api.github.com/users/ibrahimlawal/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ibrahimlawal/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ibrahimlawal/subscriptions",
"organizations_url": "https://api.github.com/users/ibrahimlawal/orgs",
"repos_url": "https://api.github.com/users/ibrahimlawal/repos",
"events_url": "https://api.github.com/users/ibrahimlawal/events{/privacy}",
"received_events_url": "https://api.github.com/users/ibrahimlawal/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "emekaonu",
"id": 3802882,
"avatar_url": "https://avatars2.githubusercontent.com/u/3802882?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/emekaonu",
"html_url": "https://github.com/emekaonu",
"followers_url": "https://api.github.com/users/emekaonu/followers",
"following_url": "https://api.github.com/users/emekaonu/following{/other_user}",
"gists_url": "https://api.github.com/users/emekaonu/gists{/gist_id}",
"starred_url": "https://api.github.com/users/emekaonu/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/emekaonu/subscriptions",
"organizations_url": "https://api.github.com/users/emekaonu/orgs",
"repos_url": "https://api.github.com/users/emekaonu/repos",
"events_url": "https://api.github.com/users/emekaonu/events{/privacy}",
"received_events_url": "https://api.github.com/users/emekaonu/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "moyheen",
"id": 8110201,
"avatar_url": "https://avatars1.githubusercontent.com/u/8110201?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/moyheen",
"html_url": "https://github.com/moyheen",
"followers_url": "https://api.github.com/users/moyheen/followers",
"following_url": "https://api.github.com/users/moyheen/following{/other_user}",
"gists_url": "https://api.github.com/users/moyheen/gists{/gist_id}",
"starred_url": "https://api.github.com/users/moyheen/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/moyheen/subscriptions",
"organizations_url": "https://api.github.com/users/moyheen/orgs",
"repos_url": "https://api.github.com/users/moyheen/repos",
"events_url": "https://api.github.com/users/moyheen/events{/privacy}",
"received_events_url": "https://api.github.com/users/moyheen/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "darmie",
"id": 848002,
"avatar_url": "https://avatars1.githubusercontent.com/u/848002?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/darmie",
"html_url": "https://github.com/darmie",
"followers_url": "https://api.github.com/users/darmie/followers",
"following_url": "https://api.github.com/users/darmie/following{/other_user}",
"gists_url": "https://api.github.com/users/darmie/gists{/gist_id}",
"starred_url": "https://api.github.com/users/darmie/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/darmie/subscriptions",
"organizations_url": "https://api.github.com/users/darmie/orgs",
"repos_url": "https://api.github.com/users/darmie/repos",
"events_url": "https://api.github.com/users/darmie/events{/privacy}",
"received_events_url": "https://api.github.com/users/darmie/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "perfectmak",
"id": 3120013,
"avatar_url": "https://avatars0.githubusercontent.com/u/3120013?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/perfectmak",
"html_url": "https://github.com/perfectmak",
"followers_url": "https://api.github.com/users/perfectmak/followers",
"following_url": "https://api.github.com/users/perfectmak/following{/other_user}",
"gists_url": "https://api.github.com/users/perfectmak/gists{/gist_id}",
"starred_url": "https://api.github.com/users/perfectmak/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/perfectmak/subscriptions",
"organizations_url": "https://api.github.com/users/perfectmak/orgs",
"repos_url": "https://api.github.com/users/perfectmak/repos",
"events_url": "https://api.github.com/users/perfectmak/events{/privacy}",
"received_events_url": "https://api.github.com/users/perfectmak/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
},
{
"login": "acekyd",
"id": 4003538,
"avatar_url": "https://avatars1.githubusercontent.com/u/4003538?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/acekyd",
"html_url": "https://github.com/acekyd",
"followers_url": "https://api.github.com/users/acekyd/followers",
"following_url": "https://api.github.com/users/acekyd/following{/other_user}",
"gists_url": "https://api.github.com/users/acekyd/gists{/gist_id}",
"starred_url": "https://api.github.com/users/acekyd/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/acekyd/subscriptions",
"organizations_url": "https://api.github.com/users/acekyd/orgs",
"repos_url": "https://api.github.com/users/acekyd/repos",
"events_url": "https://api.github.com/users/acekyd/events{/privacy}",
"received_events_url": "https://api.github.com/users/acekyd/received_events",
"type": "User",
"site_admin": false,
"score": 1.0
}
]
}
這裏是我的代碼:
try {
String total_count = jsonobject.optString("total_count");
String incomplete_results = jsonobject.optString("incomplete_results");
JSONArray jsonarray = jsonobject.getJSONArray("items");
for (int i = 0; i < 3; i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("login", jsonobject.getString("login"));
map.put("id", jsonobject.getString("id"));
//map.put("population", jsonobject.getString("population"));
map.put("avatar_url", jsonobject.getString("avatar_url"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}`
你嘗試過什麼到目前爲止? –
看起來像Github API。 Retrofit提供了一些關於查詢的例子。 https://square.github.io/retrofit/ –