Created
April 12, 2017 17:16
-
-
Save thanhcs94/e5a17a9ac92ada9bbdcd396a4c55feb6 to your computer and use it in GitHub Desktop.
convert array list to jsonarray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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