skip to Main Content

First of all sorry if the term subcategory is not the right one, I did not know what to call it

I’m trying to retrieve the data present in data but I can’t.

My Json

{
  "server": [
    {
      "data": [
        {
          "name": "Franky",
          "premium": true,
          "total": 148
        }
      ]
    }
  ]
}

Main.java

private void getData() {
  String url = "https://api.example.com/key=XXXX";

  JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
      @Override
      public void onResponse(JSONObject response) { 
          try { 
              JSONArray jsonArray = response.getJSONArray("server");
              for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject server = jsonArray.getJSONObject(i);

                  String name = data.getString("name");
                  boolean premium = data.getBoolean("premium");
                  int total = data.getInt("total");

                  UsersModel usersModel = new UsersModel(name, premium, total);
                  usersModelList.add(usersModel);

                  UsersAdapter usersAdapter = new UsersAdapter(getApplicationContext(), usersModelList);
                  recyclerUsers.setAdapter(usersAdapter);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  }, new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {
          error.printStackTrace();
      }
  });

  requestQueue.add(request);
}

I don’t get a crash but I find these error lines in logcat

org.json.JSONException: No value for name
org.json.JSONException: No value for premium
org.json.JSONException: No value for total

If you have a solution!

2

Answers


  1. In the JSON, the data property is also an array, so you need to access it as such:

    JSONArray jsonArray = response.getJSONArray("server");
    for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject serverItem = jsonArray.getJSONObject(i);
      JSONArray dataArray = serverItem.getJSONArray("data");
      for (int j = 0; i < dataArray.length(); i++) {
         JSONObject data = dataArray.getJSONObject(j);
         String name = data.getString("name");
         boolean premium = data.getBoolean("premium");
         int total = data.getInt("total");
         UsersModel usersModel = new UsersModel(name, premium, total);
         usersModelList.add(usersModel);
         UsersAdapter usersAdapter = new UsersAdapter(getApplicationContext(), usersModelList);
         recyclerUsers.setAdapter(usersAdapter);
      }
    }
    
    Login or Signup to reply.
  2. https://github.com/octomix/josson
    https://mvnrepository.com/artifact/com.octomix.josson/josson

    implementation 'com.octomix.josson:josson:1.3.20'
    
    -------------------------------------------------
    
    Josson josson = Josson.fromJsonString("{n" +
            "  "server": [n" +
            "    {n" +
            "      "data": [n" +
            "        {n" +
            "          "name": "Franky",n" +
            "          "premium": true,n" +
            "          "total": 148n" +
            "        }n" +
            "      ]n" +
            "    }n" +
            "  ]n" +
            "}");
    System.out.println(josson.getNode("server.data")); // -> [{"name":"Franky","premium":true,"total":148}]
    System.out.println(josson.getNode("server.data.name")); // -> ["Franky"]
    System.out.println(josson.getNode("server.data.premium")); // -> [true]
    System.out.println(josson.getNode("server.data.total")); // -> [148]
    System.out.println(josson.getNode("server.data[0]")); // -> {"name":"Franky","premium":true,"total":148}
    System.out.println(josson.getNode("server.data[0].name")); // -> "Franky"
    System.out.println(josson.getNode("server.data[0].premium")); // -> true
    System.out.println(josson.getNode("server.data[0].total")); // -> 148
    System.out.println(josson.getNode("server.data.name[0]")); // -> "Franky"
    System.out.println(josson.getNode("server.data.premium[0]")); // -> true
    System.out.println(josson.getNode("server.data.total[0]")); // -> 148
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search