Parsing json to string in Gson
I have a json string:
{
"id":123,
"name":"",
"details":{}
}
I want to parse this object:
class Student {
int id;
String name;
String details;
}
This is the error I am getting:
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx/xxx.xxx.MainActivity}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
The reason I want to drill down as a string not as a JsonObject is because I am using a Realm DB object and storing that in the database. How can I tell Gson that I want a string details
as a string.
source to share
Value details
, {}
is the object, not a string. It won't be interpreted as a string unless you quote it like this:
{
"id":123,
"name":"",
"details":"{}"
}
GSON tells you " Expected STRING but was BEGIN_OBJECT
". This makes sense because you are giving it a type signature String
with a named attribute details
, but your serialization has a named attribute details
that contains an empty object.
source to share