Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JSONObject : add Array to the put method

// JSON object to hold the information, which is sent to the server
JSONObject jsonObjSend = new JSONObject();
jsonObjSend.put("action", "myAction");
jsonObjSend.put("type", tipo);

For now is everything ok but if I want to add

jsonObjSend.put("elementi", arrayOfElements);

where arrayOf Elements must be an array of strings. How can I do?

/** EDIT

EXAMPLE OF WHAT I NEED

{
  "action": "myAction",
  "type": "elementi",
  "elementi": [
    "3287498357",
    "23472857"
  ]
}
like image 317
Usi Usi Avatar asked Sep 01 '13 21:09

Usi Usi


1 Answers

After seeing the example I understood that you are trying to do something similar as asked in Java JsonObject array value to key

jsonObjSend.put("elementi", new JSONArray(new Object[] { "value1", "value2", "value3"} ));

To simplify:

JSONArray arr = new JSONArray();
arr.put("value1");
arr.put("value2");
//...
jsonObjSend.put("elementi", arr);
like image 62
Shobhit Puri Avatar answered Oct 12 '22 13:10

Shobhit Puri