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.