在android中解析json,只对一种对象感兴趣的无名数组

时间:2014-04-09 06:52:36

标签: android json

我如何解析此json截至目前我只对用户对象感兴趣但后来我可能需要解析其他对象以便将来更改应该很容易

[ { "url": "https://api.github.com/gists/10232136", "forks_url": "https://api.github.com/gists/10232136/forks", "commits_url": "https://api.github.com/gists/10232136/commits", "id": "10232136", "git_pull_url": "https://gist.github.com/10232136.git", "git_push_url": "https://gist.github.com/10232136.git", "html_url": "https://gist.github.com/10232136", "files": { "gulpfile-viget-pattern.js": { "filename": "gulpfile-viget-pattern.js", "type": "application/javascript", "language": "JavaScript", "raw_url": "https://gist.githubusercontent.com/andrewmartin/10232136/raw/72c67881fe717ecf35dd2a0f4524fca989b9b643/gulpfile-viget-pattern.js", "size": 426 } }, "public": true, "created_at": "2014-04-09T06:34:16Z", "updated_at": "2014-04-09T06:34:16Z", "description": "gulpfile-viget-pattern-test.js", "comments": 0, "user": { "login": "andrewmartin", "id": 273842, "avatar_url": "https://avatars.githubusercontent.com/u/273842?", "gravatar_id": "3377b3e694bdacac127ca9ee6c871c52", "url": "https://api.github.com/users/andrewmartin", "html_url": "https://github.com/andrewmartin", "followers_url": "https://api.github.com/users/andrewmartin/followers", "following_url": "https://api.github.com/users/andrewmartin/following{/other_user}", "gists_url": "https://api.github.com/users/andrewmartin/gists{/gist_id}", "starred_url": "https://api.github.com/users/andrewmartin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/andrewmartin/subscriptions", "organizations_url": "https://api.github.com/users/andrewmartin/orgs", "repos_url": "https://api.github.com/users/andrewmartin/repos", "events_url": "https://api.github.com/users/andrewmartin/events{/privacy}", "received_events_url": "https://api.github.com/users/andrewmartin/received_events", "type": "User", "site_admin": false }, "comments_url": "https://api.github.com/gists/10232136/comments" }, { "url": "https://api.github.com/gists/10232076", "forks_url": "https://api.github.com/gists/10232076/forks", "commits_url": "https://api.github.com/gists/10232076/commits", "id": "10232076", "git_pull_url": "https://gist.github.com/10232076.git", "git_push_url": "https://gist.github.com/10232076.git", "html_url": "https://gist.github.com/10232076", "files": { "ScrollableGridView.java": { "filename": "ScrollableGridView.java", "type": "text/plain", "language": "Java", "raw_url": "https://gist.githubusercontent.com/kumarvmsathish/10232076/raw/490b09b86dc7d154d12dd754a1cb0ac47276343c/ScrollableGridView.java", "size": 1399 } }, "public": true, "created_at": "2014-04-09T06:33:20Z", "updated_at": "2014-04-09T06:33:20Z", "description": null, "comments": 0, "user": { "login": "kumarvmsathish", "id": 5295401, "avatar_url": "https://avatars.githubusercontent.com/u/5295401?", "gravatar_id": "d85ebb855467b4c0aa03d4b394a3396f", "url": "https://api.github.com/users/kumarvmsathish", "html_url": "https://github.com/kumarvmsathish", "followers_url": "https://api.github.com/users/kumarvmsathish/followers", "following_url": "https://api.github.com/users/kumarvmsathish/following{/other_user}", "gists_url": "https://api.github.com/users/kumarvmsathish/gists{/gist_id}", "starred_url": "https://api.github.com/users/kumarvmsathish/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/kumarvmsathish/subscriptions", "organizations_url": "https://api.github.com/users/kumarvmsathish/orgs", "repos_url": "https://api.github.com/users/kumarvmsathish/repos", "events_url": "https://api.github.com/users/kumarvmsathish/events{/privacy}", "received_events_url": "https://api.github.com/users/kumarvmsathish/received_events", "type": "User", "site_admin": false }, "comments_url": "https://api.github.com/gists/10232076/comments" }, { "url": "https://api.github.com/gists/10232038", "forks_url": "https://api.github.com/gists/10232038/forks", "commits_url": "https://api.github.com/gists/10232038/commits", "id": "10232038", "git_pull_url": "https://gist.github.com/10232038.git", "git_push_url": "https://gist.github.com/10232038.git", "html_url": "https://gist.github.com/10232038", "files": { "osx_oracle_client.md": { "filename": "osx_oracle_client.md", "type": "text/plain", "language": "Markdown", "raw_url": "https://gist.githubusercontent.com/usutani/10232038/raw/fc7616e65fbddf52e3735016fb86ba79d6e7e06f/osx_oracle_client.md", "size": 172 } }, "public": true, "created_at": "2014-04-09T06:32:46Z", "updated_at": "2014-04-09T06:34:55Z", "description": "OS X Mavericks に Oracle Client をインストール", "comments": 1, "user": { "login": "usutani", "id": 56579, "avatar_url": "https://avatars.githubusercontent.com/u/56579?", "gravatar_id": "c702d3fad57bde7ba189eec1b4c593e3", "url": "https://api.github.com/users/usutani", "html_url": "https://github.com/usutani", "followers_url": "https://api.github.com/users/usutani/followers", "following_url": "https://api.github.com/users/usutani/following{/other_user}", "gists_url": "https://api.github.com/users/usutani/gists{/gist_id}", "starred_url": "https://api.github.com/users/usutani/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/usutani/subscriptions", "organizations_url": "https://api.github.com/users/usutani/orgs", "repos_url": "https://api.github.com/users/usutani/repos", "events_url": "https://api.github.com/users/usutani/events{/privacy}", "received_events_url": "https://api.github.com/users/usutani/received_events", "type": "User", "site_admin": false }, "comments_url": "https://api.github.com/gists/10232038/comments" },
}
]

4 个答案:

答案 0 :(得分:1)

因为这是一个没有名字的jsonArray

请尝试以下操作:

  1. 首先从结果中获取JsonArray。

    JSONArray json = new JSONArray(your_json_string);

  2. 然后从jsonobject获取单个jsonarray,如下所示:

    int arrayLength = json.length();
    
    for(int i=0;i<arrayLength;i++){   
    
     // Get the single object in the array
     JSONObject singleObject = json.getJSONObject(i);
    
     if (singleObject.has("user")) {
    
        JSONObject userObject = singleObject.getJSONObject("user");
            // TODO: retrieve user jsonobjects data
     }           
    }
    

答案 1 :(得分:0)

尝试以下

String user;
JSONArray data = json.getJSONArray(YOUR_ARRAY); //json is JSONObject
JSONObject object = data.getJSONObject(0);
user = object.getString("user");

答案 2 :(得分:0)

试试这个..

JSONArray jsonarray = new JSONArray(response);

for (int i=0; i < jsonarray.length(); i++){
    JSONObject jsob = jsonarray.getJSONObject(i);
    String url = jsob.getString("url");
    //same for remaining like url,forks_url etc.,
    JSONObject files_obj = jsob.getJSONObject("files").getJSONObject("gulpfile-viget-pattern.js");
    String type = files_obj.getString("type");
    //same for remaining like type,language etc.,
    JSONObject user_obj = jsob.getJSONObject("user");
    String login = user_obj.getString("login");     
    //same for remaining like login,url etc.,   
}

答案 3 :(得分:0)

在您的Json响应中,您的第一个标记为JSONArray。所以你需要解析像

这样的数据
 JSONArray jarray = new JSONArray(Your_Response);

 for (int i=0; i < jarray.length(); i++){
   JSONObject jb= jarray.getJSONObject(i);
   String url = jb.optString("url");

   JSONObject other_jb= jb.getJSONObject("files").getJSONObject("gulpfile-viget-pattern.js");
   String type = other_jb.optString("type");

   JSONObject jsonObject_of_user = jb.getJSONObject("user");
   String user_login = jsonObject_of_user.optString("login");     

 }