0

i am supposed to receive a JSON like this

{
    "browse": [
        {
            "title": "Program Translations"
        }
    ],
    "errorno": "fe_200",
    "message": "Your browse history"
}

But due to no browse information, i get it like this..

{"errorno":"fe_3946","message":"No browse history found.","browse":"NULL"}

which is perfectly normal.

i have written java class like this..

class BrowseHistoryJson {

      public BrowseHistoryJson() {}

      private String errorno;
      private String message;
      private List<Browse> browse;
      // other attributes

      public String getErrorNo() {
        return errorno;
      }
      public String getMessage() {
            return message;
          }
      public List<Browse> getResults() { return browse; }
      public void setErrorNo(String errorno) {
            this.errorno = errorno;
          }
      public void setMessage(String message) {
        this.message = message;
      }

      public void setResults(List<Browse> browse) { this.browse = browse; }
      public String toString() { return "Results[" + browse + "]";}


      static class Browse {
            private String title;


            public String getTitle() {
                return title;
            }


            public void setTitle(String title) {
                this.title = title;
            }


            public String toString() { return "Result[title:" + title +"]"; }
        }

    }

Now when i execute the statement

BrowseHistoryJson results = new Gson().fromJson(reader, BrowseHistoryJson.class);

i am getting exception like

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 74   at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:734)

it is working perfectly fine, when i get data in array in browse.

how to add null handling, like some way to declare beforehand that this value may come as null?

i checked JSON notes and searched on internet, but nothing really helpful.

1 Answer 1

1

You need to use a GsonBuilder object to create your Gson object.

Gson gson = new GsonBuilder().serializeNulls().create();//handle null

When you do it this way, the gson will treat nulls appropriately.

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

2 Comments

thanks @crstamps2, yes, saw the document gson doc it should do the job. and it should be ofcourse Gson gson = new GsonBuilder().serializeNulls().create(); but i am still getting same exception, but i saw it carefully, i am getting "browse":"NULL", i just wonder, my JSON sender hasn't prepared it well for putting null value to be serialized, what do you think?
its not necessarily a bad thing if the JSON has that null value. You can handle the null on the other end. It's preference though really. One of the great things about JSON is the unstructured nature of it.

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.