4

I am using GSON library for turing JSON that comes form a web service but I can't make it work, I always get a null. I've looked through similar issues like Converting Json to Java such as Simple Json to Java convertion using GSON. But I am still missing something

JSON

{"A":"val1","B":"val2","C":"val3","D":"val4","E":"val5","F":"val6","G":"val7"}
         SiteWrapper m = gson.fromJson(json, SiteWrapper.class);

java Class

SiteWrapper m = gson.fromJson(json, SiteWrapper.class);
System.out.println(m.getMenu());

static class Site {
    static String A;
    static String B;
    static String C;
    static String D;
    static String E;
    static String F;
    static String G;

    public String toString() {
        return String.format(A,B,C,D,E,F,G);}

    public static String getA() {
        return A;
    }
    public static String getB() {
        return B;
    } 
... all the way to getG

    public void setA(String A) {
        Site.A = A;
    }
    public void setB(String B) {
        Site.B = B;
    }
... all the way to setB

and my wrapper

class SiteWrapper {
    private Site site;
    public Site getMenu() { return site; }
    public void setMenu(Site site) { this.site = site; }
}

no matter what I do I get a null printed , any ideas?

Community
  • 1
  • 1
Quantico
  • 2,398
  • 7
  • 35
  • 59

3 Answers3

2

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

 Gson gson = new GsonBuilder()
    .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

Community
  • 1
  • 1
wtsang02
  • 18,603
  • 10
  • 49
  • 67
0

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

Mark Meyer
  • 3,643
  • 3
  • 23
  • 34
  • While this is certainly contributing to the issue, this will not work by itself. Statics are excluded by default. – rmlan Jan 16 '13 at 22:04
0

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:

Gson gson = new GsonBuilder()
    .excludeFieldsWithModifier(Modifier.TRANSIENT,Modifier.VOLATILE)
    .create();

This should create a Gson object that will not exclude static fields by default.

rmlan
  • 4,387
  • 2
  • 21
  • 28
  • 1
    You'll need to make both the changes suggested in NuclearGhost's answer, and mine for it work properly. – rmlan Jan 16 '13 at 22:14