Skip to content

Instantly share code, notes, and snippets.

@thanhcs94
Created April 12, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanhcs94/e5a17a9ac92ada9bbdcd396a4c55feb6 to your computer and use it in GitHub Desktop.
Save thanhcs94/e5a17a9ac92ada9bbdcd396a4c55feb6 to your computer and use it in GitHub Desktop.
convert array list to jsonarray
public class ListItem {
private long _masterId;
private String _name;
private long _category;
public ListItem(long masterId, String name, long category) {
_masterId = masterId;
_name = name;
_category = category;
}
public JSONObject getJSONObject() {
JSONObject obj = new JSONObject();
try {
obj.put("Id", _masterId);
obj.put("Name", _name);
obj.put("Category", _category);
} catch (JSONException e) {
trace("DefaultListItem.toString JSONException: "+e.getMessage());
}
return obj;
}
}
//
ArrayList<ListItem> myCustomList = ArrayList<ListItem>();
JSONArray jsonArray = new JSONArray();
for (int i=0; i < myCustomList.size(); i++) {
jsonArray.put(myCustomList.get(i).getJSONObject());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment