0

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?

5
  • can you do it in a JSONArray maybe? A map isnt an array. Commented Sep 12, 2018 at 15:37
  • Could you demonstrate what you mean, been a long day haha Commented Sep 12, 2018 at 15:38
  • 1
    already?! give me a few minutes please. Commented Sep 12, 2018 at 15:38
  • Sorry haha..... Commented Sep 12, 2018 at 15:39
  • Please show a minimal reproducible example. Be sure to include all variable declarations (such as for device) and all relevant parts of each class. Also explain exactly what end result you require. Commented Sep 12, 2018 at 16:02

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.