I'm writing an android app that requires binding a JSON object into a domain entity with the keys from the JSON as instance variables. Since there are several domain entities, each with different instance variables that the JSON needs to bind to in the app, i'd like to write a method such as ths following:
The reason i'm interested in the doing the binding from the class to the JSON is because if the JSON changes for some reason, I don't want it to break the app when the instance variable doesn't exist in the app's domain for a specific JSON key.
Thanks in advance for any help!
Most likely you want to use a json library that does this for you. Jackson is a good one that is also used by Spring MVC.
To answer your question, you can use getDeclaredFields()
on a class, and use java.lang.reflect.Modifier.isStatic()
to check if the field is static.
private static class Foo {
private static long car;
private int bar;
private String baz;
}
public static void main(String[] args) {
for (Field field : Foo.class.getDeclaredFields()) {
if (!Modifier.isStatic(field.getModifiers())) {
System.out.println("Found non-static field: " + field.getName());
}
}
}
This will print out:
Found non-static field: bar
Found non-static field: baz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With