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?

1
  • "all the way to setB" I think you mean "setG"? Commented Jan 16, 2013 at 22:00

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

While this is certainly contributing to the issue, this will not work by itself. Statics are excluded by default.
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.

1 Comment

You'll need to make both the changes suggested in NuclearGhost's answer, and mine for it work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.