-2

How do I iterate through a JSON object to get a JSON array in android my code keeps returning no value in my JSON array and I think it's because it's not the first item in my JSON object cause most JSON arrays I work with re usually the first item in their JSON object ......the code works perfectly when I replace the JSON object ... I just need to know how I can get "items" ...please

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

here is my code :

        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;
    }`
Noel Obaseki
  • 60
  • 1
  • 4
  • 16

4 Answers4

1

you can get your object like following.

try {
            JSONObject jsonObject = new JSONObject("Your json string");
            String total_count = jsonObject.optString("total_count");
            String incomplete_results = jsonObject.optString("incomplete_results");
            JSONArray array = jsonObject.getJSONArray("items");

            if (array != null && array.length() >0){
                for (int i = 0; i < array.length(); i++) {
                   JSONObject object = array.getJSONObject(i);
                    String id = object.getString("id");
                    .
                    .
                    .

                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
0

let your JSON is stored in variable json.

JSONArray items = json.getJSONArray("items");
for (int i=0; i<items.length(); i++) {
    JSONObject item = items.getJSONObject(i);
    // TODO: get values from 'item'
}

If you want only first item:

JSONArray items = json.getJSONArray("items");
JSONObject item = items.getJSONObject(0);
Ravi Sisodia
  • 776
  • 1
  • 5
  • 20
0

Use this

if your file is stored in response variable

SONObject json=new JSONObject(response); //here get your json data
JSONArray jar=json.getJSONArray("items");
for (int i=0; i<jar.length(); i++) {
    JSONObject items = jar.getJSONObject(i);// get values from 'items'
    String item=items.getString("login"); //get login like this

}
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
0

Try this,

            JSONObject obj=new JSONObject(response);
            int total_count=obj.getInt("total_count");
            boolean incomplete_results=obj.getBoolean("incomplete_results");

            JSONArray items=obj.getJSONArray("items");
            for(int i=0;i<items.length();i++)
            {
                JSONObject items_obj=items.getJSONObject(i);
                String login=items_obj.getString("login");
                int id=items_obj.getInt("id");
                String avatar_url=items_obj.getString("avatar_url");
                String gravatar_id=items_obj.getString("gravatar_id");
                String url=items_obj.getString("String");
                String html_url=items_obj.getString("html_url");
                String followers_url=items_obj.getString("followers_url");
                String type=items_obj.getString("type");
            }
Komal12
  • 3,340
  • 4
  • 16
  • 25