Json To Java Object Using Gson
Solution 1:
Since its a static inner class .As docs pointed out and comments :
As well, if a field is marked as "static" then by default it will be excluded. If you want to include some transient fields...
You may want to try
Gsongson=newGsonBuilder()
.excludeFieldsWithModifier()
.create();
Also, since its a inner class you may need to change your JSON If you can:
{
"site":{
"A":"val1",
"B":"val2",
"C":"val3",
"D":"val4",
"E":"val5",
"F":"val6",
"G":"val7"
}
}
As noted here in this post
Solution 2:
The issue is that in your code you're passing SiteWrapper.class
when you should be passing Site.class
to gson.fromJSON
This line
SiteWrapper m = gson.fromJson(json, SiteWrapper.class);
should be
Site s = gson.fromJSON(json, Site.class);
Site
is the class you defined for the JSON provided. SiteWrapper
contains a site variable, you need to set this Site
variable to the result of the fromJSON
Solution 3:
Per this documentation, all static fields are excluded by default. Follow the example in the link to alter the default exclusion strategy so that statics are accepted.
When you create your Gson
object, try the following:
Gsongson=newGsonBuilder()
.excludeFieldsWithModifier(Modifier.TRANSIENT,Modifier.VOLATILE)
.create();
This should create a Gson
object that will not exclude static fields by default.
Post a Comment for "Json To Java Object Using Gson"