I'm trying to create an instance of an object from a json string. This is my object:
public class Person {
String name;
String address;
}
and this is my converter:
Gson gson = new Gson();
Person p = gson.fromJson(str, Person.class);
The problem is that my input string format can be more complex than my Person object, for example:
{
"name":"itay",
"address":{
"street":"my street",
"number":"10"
}
}
Or address
's value can be a simple string (in that case I have no problem).
I want p.address
to contain the json object as string.
This is only an example of my problem, in fact, the "address" is much more complex and the structure is unknown.
My solution is changing the Person
class to:
public class BetterPerson {
String name;
Object address;
}
Now, address
is an object and I can use .toString()
to get the value.
Is there a better way of doing this?