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
In the JSON, the
data
property is also an array, so you need to access it as such:https://github.com/octomix/josson
https://mvnrepository.com/artifact/com.octomix.josson/josson