Reputation: 769
I would like to create JSON format and I have some problem about this
I have no idea to create Jsonarray
and JsonObject
in JsonObject
{
"users": [7, 16, 35],
"group_id": "sdkfjsdkljflds"
}
I try
JSONObject jsonParams = new JSONObject();
try {
jsonParams.put("group_id", "dlfsdds");
jsonParams.put("users", list);
} catch (JSONException e) {
e.printStackTrace();
}
and my log is show
{
"users":"[7, 16, 35]",
"group_id":"dlfsdds"
}
ps. list is from
for (int k=0;k<allMember.size();k++){
list.add(allMember.get(k));
}
what is my mistake? and How to fix it?
thank for your help:D
Upvotes: 1
Views: 449
Reputation: 22233
You need to convert the list to JSONArray
first:
try {
jsonParams.put("group_id", "dlfsdds");
JSONArray listJson = new JSONArray();
for(int i=0; i<list.size(); i++) {
listJson.put(list.get(i));
}
jsonParams.put("users", listJson);
} catch (JSONException e) {
e.printStackTrace();
}
Most libraries also allow you to avoid the for-loop and simply do:
JSONArray listJson = new JSONArray(list);
Upvotes: 2