Is there a native way in kotlin to access a nested complex object (parsed from JSON string) through a variable?
Smth similar to:
var = "Obj4"
a = Obj1.Obj2.Obj3.$var.Obj5.Array[index]
Thanks a lot in advance
Is there a native way in kotlin to access a nested complex object (parsed from JSON string) through a variable?
Smth similar to:
var = "Obj4"
a = Obj1.Obj2.Obj3.$var.Obj5.Array[index]
Thanks a lot in advance
Taken from here
Using the reflection. Don't forget to add the dependency. Global inline extension fun for Any:
inline fun <reified T : Any> Any.getThroughReflection(propertyName: String): T? {
val getterName = "get" + propertyName.capitalize()
return try {
javaClass.getMethod(getterName).invoke(this) as? T
} catch (e: NoSuchMethodException) {
null
}
}