Lewis Smith
Lewis Smith

Reputation: 1345

Java create nested array, one array containing and array of arrays with key value pairs

I am trying to create an array to post to my PHP api from my android application.

I have set up the php end to expect an array that matches this format:

$post = [
    "checks" => [
        [
            "check_id" => $check->id,
            "completed" => true
        ],
        [
            "check_id" => $checkTwo->id,
            "completed" => true
        ]
    ]
];

So I need to recreate this on the java end.

I have an array of the checks which I am looping through:

for(DeviceCheck check : device.checks()){

}

And have tried to use JsonObjects and JsonArray but just can't get the end result I require.

I have also tried using Map like this:

Map<String, String> map = new HashMap<String, String>();
map.put("check_id", String.valueOf(checkId));
map.put("completed", String.valueOf(check.completed));

But then couldn't figure out how to apply this to the checks array that I need.

Any suggestions to get this?

Upvotes: 0

Views: 693

Answers (1)

letsCode
letsCode

Reputation: 3064

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

Upvotes: 2

Related Questions