/** * Construct a JSONObject from an Object, using reflection to find the public members. The * resulting JSONObject's keys will be the strings from the names array, and the values will be * the field values associated with those keys in the object. If a key is not found or not * visible, then it will not be copied into the new JSONObject. * * @param object An object that has fields that should be used to make a JSONObject. * @param names An array of strings, the names of the fields to be obtained from the object. */ public JSONObject(Object object, String names[]) { this(); Class c = object.getClass(); for (int i = 0; i < names.length; i += 1) { String name = names[i]; try { putOpt(name, c.getField(name).get(object)); } catch (Exception ignore) { } } }