Create a checks model.
class Checks {
String check;
boolean completed;
Checks (String check, boolean completed) {
this.check = check;
this.completed = completed;
}
public String getCheck() {
return check;
}
public void setCheck(String check) {
this.check = check;
}
public boolean getCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
}
Add items into your array... iterate through it, create a new object, insert items into object, add object to array.
List<Checks> list = new ArrayList<>();
list.add(new Checks("check1", true));
list.add(new Checks("check2", false));
list.add(new Checks("check3", false));
list.add(new Checks("check4", true));
list.add(new Checks("check5", true));
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
try {
for (int i = 0; i < list.size(); i++) {
JSONObject checkobjects = new JSONObject();
checkobjects.put("check_id", list.get(i).getCheck());
checkobjects.put("completed", list.get(i).getCompleted());
array.put(checkobjects);
}
object.put("checks", array);
} catch (JSONException e1) {
e1.printStackTrace();
}
System.out.println(object);
}
This will print the following:
{"checks":[
{"check_id":"check1","completed":true},
{"check_id":"check2","completed":false},
{"check_id":"check3","completed":false},
{"check_id":"check4","completed":true},
{"check_id":"check5","completed":true}
]}
Oh, and finally, if you want to get the information from the server object, you do the following...
try {
JSONArray getArr = object.getJSONArray("checks");
for (int i = 0; i < getArr.length(); i++) {
System.out.println("check_id: " + getArr.getJSONObject(i).getString("check_id") + " " + "completed: " + getArr.getJSONObject(i).getBoolean("completed"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Doing that will print the following:
check_id: check1 completed: true
check_id: check2 completed: false
check_id: check3 completed: false
check_id: check4 completed: true
check_id: check5 completed: true
device) and all relevant parts of each class. Also explain exactly what end result you require.