如何从JSONArray获取数据

时间:2019-04-11 13:59:00

标签: android json android-volley

我是android编程的新手,我正尝试从此JSON获取数据

{
  "user": {
    "username": "justin",
    "private": false,
    "name": "Justin Nemeth",
    "vip": true,
    "vip_ep": false,
    "ids": {
      "slug": "justin"
    },
    "gender": "male",
    "age": 32,
    "images": {
      "avatar": {
        "full": 
"https://secure.gravatar.com/avatar/30c2f0dfbc39e48656f40498aa871e33?r=pg&s=256"
      }
    }
}

我需要获取的只是"username""full"

我尝试使用jsonObject.getString("username"),但是它没有用,并且我做了更多的研究,我发现我应该改用JSONArray,但是我尝试了一下,但它仍然对我不起作用。我知道我做错了什么,但我不知道是什么。

这是我的代码

final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
            Request.Method.GET,
            userDetailsUri,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {

                        JSONObject mainObject = new JSONObject(response);
                        JSONArray resArray = mainObject.getJSONArray("user");

                        for (int i = 0; i < resArray.length(); i++) {

                            JSONObject jsonObject = resArray.getJSONObject(i);

                            UserDetails userDetails = new UserDetails();
                            userDetails.setUsername(jsonObject.getString("username"));
                            userDetails.setProfile_image(jsonObject.getString("full"));

                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("vkv", "getUserDetails() " + error.toString());
                }
            })
    {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {

            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", "Bearer " + access_token);
            headers.put("trakt-api-version", "2");
            headers.put("trakt-api-key", client_id);
            return headers;
        }
    };
    requestQueue.add(jsonObjectRequest);

2 个答案:

答案 0 :(得分:1)

.additional_details { overflow: hidden; width: 20px; text-overflow: ellipsis; } 是JsonObject而不是JsonArray,因此您可以像下面这样获得名称和完整信息

user

答案 1 :(得分:0)

尽管使用上述答案后我仍然遇到错误,但我还是以此为基础解决了我的问题。这是代码

JSONObject mainObject = new JSONObject(response.toString());
                        JSONObject jsonObject = mainObject.getJSONObject("user");
                        JSONObject object = jsonObject.getJSONObject("images");
                        JSONObject jsonObject1 = object.getJSONObject("avatar");

                        String image = jsonObject1.getString("full");
                        String username = jsonObject.getString("username");