Android JSONObject: add an array to 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); 

So far so good, but if I want to add

 jsonObjSend.put("elementi", arrayOfElements); 

where arrayOf Elements should be an array of strings. How can i do

/ ** EDIT

EXAMPLE WHAT NEEDS

 { "action": "myAction", "type": "elementi", "elementi": [ "3287498357", "23472857" ] } 
+6
source share
2 answers

After looking at the example, I realized that you are trying to do something similar, as given in the value of the Java JsonObject array, to the 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); 
+29
source
 JSONObject jsonBody = new JSONObject(); jsonBody.put("action", "myAction"); //action is your string jsonBody.put("type", "elementi"); JSONArray arr = new JSONArray(); JSONObject elementi= new JSONObject(); itemList.put("id", id); itemList.put("name", name); arr.put(elementi); jsonBody.put("elementi", arr); 
0
source

Source: https://habr.com/ru/post/952936/


All Articles