I am trying to create this JSON object on android. I am stuck on how to add a string array in the object.
A = {
"class" : "4" ,
"name" : ["john", "mat", "jason", "matthew"]
}
This is the code that I have written :
import org.json.JSONObject;
JSONObject school = new JSONObject();
school.put("class","4");
school.put("name", ["john", "mat", "jason", "matthew"] );
But the last line gives an error. Any way past this?
Little improper approach suggested by Tom. Optimised code would be:
ArrayList<String> list = new ArrayList<String>();
list.add("john");
list.add("mat");
list.add("jason");
list.add("matthew");
JSONObject school = new JSONObject();
school.put("class","4");
school.put("name", new JSONArray(list));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With