How to set JSON Array values to seter class in Android Studio with volley
Solution 1:
try {
JSONObject obj = new JSONObject(response);
//if no error in response
if (!obj.getBoolean("error")) {
//getting Status from response
JSONArray statusJson = obj.getJSONArray("status");
for (int i = 0; i < statusJson.length(); i++) {
Status status = new Status(
statusJson.getJSONObject(i).getString("OrderCode"),
statusJson.getJSONObject(i).getString("GuestName"),
statusJson.getJSONObject(i).getString("ProductName"),
statusJson.getJSONObject(i).getString("ProductType"),
statusJson.getJSONObject(i).getString("NoTable"),
statusJson.getJSONObject(i).getString("Status"),
statusJson.getJSONObject(i).getString("TotalPrice")
);
// Add Status In your Array list and enjoy
}
} else {
Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Solution 2:
You can use Gson library and convert your json to a model class
To do it first you have to add gson as a dependancy
implementation "com.google.code.gson:gson:2.8.6"
And then since your status in json is array use it like this
JSONArray status = obj.getJSONArray("status");
//Code to convert json array to arraylist of object
Arraylist<Status> arraylist = Gson().fromJson(
status.toString(),
new TypeToken<ArrayList<Status>>(){}.getType()
)
Above code will store your json array status to arraylist variable