通过JSON对象android迭代

时间:2017-03-09 09:43:14

标签: android arrays json

如何迭代JSON对象以获取android中的JSON数组 我的代码在我的JSON数组中不会返回任何值,我认为这是因为它不是我的JSON对象中的第一项,因为我使用的大多数JSON数组通常是其JSON对象中的第一项。 ....当我替换JSON对象时代码工作正常...我只需要知道如何获得" items" ......请

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;
    }`

4 个答案:

答案 0 :(得分:0)

让您的JSON存储在变量json

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

如果您只想要第一个item

JSONArray items = json.getJSONArray("items");
JSONObject item = items.getJSONObject(0);

答案 1 :(得分:0)

使用此

如果您的文件存储在response变量

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

}

答案 2 :(得分:0)

你可以像以下一样得到你的对象。

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

答案 3 :(得分:0)

试试这个,

            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");
            }
相关问题