简体   繁体   English

如何从 JSONArray 获取数据

[英]How to get data from JSONArray

I am new to android programming and I'm trying to get data from this JSON我是 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"
      }
    }
}

All I need to get is the "username" and "full"我需要得到的只是"username""full"

I tried using jsonObject.getString("username") , but it did not work and I did more research and I found that I should use JSONArray instead, but I tried it and it still did not work for me.我尝试使用jsonObject.getString("username") ,但它没有用,我做了更多的研究,我发现我应该使用JSONArray代替,但我尝试了它,但它仍然对我不起作用。 I know I'm doing something wrong but I can't tell what it is.我知道我做错了什么,但我不知道是什么。

Here's my code这是我的代码

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

user is JsonObject not JsonArray so you can get name and full like below user是 JsonObject 不是 JsonArray 所以你可以得到名称和完整的如下

       JSONObject mainObject = new JSONObject();

        JSONObject jobjUser =mainObject.getJSONObject("user");

        String username=jobjUser.getString("username");

        JSONObject jobjImages=mainObject.getJSONObject("images");
        JSONObject jobjAvatar=jobjImages.getJSONObject("avatar");
        String full=jobjAvatar.getString("full");

Although I still had errors after using the answer above, I used it as a basis to fix my issue.虽然我在使用上面的答案后仍然有错误,但我用它作为解决我问题的基础。 Here's the code这是代码

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM