Skip to content

Instantly share code, notes, and snippets.

@januprasad
Created November 26, 2014 05:27
Show Gist options
  • Save januprasad/36590a6669270fcd0ae7 to your computer and use it in GitHub Desktop.
Save januprasad/36590a6669270fcd0ae7 to your computer and use it in GitHub Desktop.
getArraylistModelClass to get model class arraylist from a jsonarray
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
public List<ModelClass> getArraylistModelClass(String result) {
ArrayList<ModelClass> items = new ArrayList<ModelClass>();
// TODO Auto-generated method stub
boolean status = false;
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
JSONArray businesJSON = jsonObject.getJSONArray("arrayName");
for (int i = 0; i < businesJSON.length(); i++) {
JSONObject obj = businesJSON.getJSONObject(i);
Gson gson = new Gson();
ModelClass theme = gson.fromJson(obj.toString(),
ModelClass.class);
for (Field field : theme.getClass().getDeclaredFields()) {
field.setAccessible(true);
String name = field.getName();
Object value;
try {
value = field.get(theme);
// System.out.printf("Field name: %s, Field value: %s%n",
// name,
// value);
// if (pref.getPrefs(name.toString()).length() < 2)
// pref.setPrefs(name.toString(), value.toString());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
items.add(theme);
status = true;
}
if (status)
return items;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment