I am trying to create an instance of an object from a json string. It is my goal:
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 may be more complex than my Person object, for example:
{
"name":"itay",
"address":{
"street":"my street",
"number":"10"
}
}
Or the addressvalue could be a simple string (in this case, I have no problem). I want to p.addresscontain the json object as a string. This is just an example of my problem, because the "address" is much more complicated and the structure is unknown.
My solution changes the class Personto:
public class BetterPerson {
String name;
Object address;
}
Now addressthis is an object, and I can use .toString()to get the value.
Is there a better way to do this?