Reputation: 218
This is my class which represents the json-structure from the server:
ArrayList<EventWebInterface.Element> element;
public class Element {
String id;
String subtitle;
String name;
String subline;
String count;
}
And this is the structure:
{
element: [
{
id: "1985",
subtitle: "01. August 2013 18:30 Uhr | Berlin",
name: "blabla....",
subline: "blabla....",
category_id: ""
},
{
id: "1962",
subtitle: "07. August 2013 19:00 Uhr | Cloppenburg",
name: "blabla...",
subline: "blabla....",
category_id: ""
},
{ ...
But sometimes the structure has only one element like:
{
element: {
id: "1985",
subtitle: "01. August 2013 18:30 Uhr | Berlin",
name: "blabla...",
subline: "blabla....",
category_id: ""
}
}
For this case i've got a typeadapter for gson which looks like this:
public class EventWebTypeAdapter implements JsonDeserializer<List<EventWebInterface.Element>> {
public List<EventWebInterface.Element> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) {
List<EventWebInterface.Element> vals = new ArrayList<EventWebInterface.Element>();
if (json.isJsonArray()) {
for (JsonElement e : json.getAsJsonArray()) {
vals.add((EventWebInterface.Element) ctx.deserialize(e, EventWebInterface.Element.class));
}
} else if (json.isJsonObject()) {
vals.add((EventWebInterface.Element) ctx.deserialize(json, EventWebInterface.Element.class));
} else {
throw new RuntimeException("Unexpected JSON type: " + json.getClass());
}
return vals;
}
public Type getType(){
return new TypeToken<List<EventWebInterface.Element>>() {}.getType();
}
}
I use it this way:
EventWebTypeAdapter mTypeAdapter = new EventWebTypeAdapter();
Type mType = mTypeAdapter.getType();
Class<I> mInterface = EventWebInterface.class
Gson gson = new GsonBuilder().registerTypeAdapter(mType, mTypeadapter).create();
dataObject = gson.fromJson(data, mInterface);
But the Adapter doesn't work. It gives me:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
I don't get it, why it doesn't work... =(
Upvotes: 1
Views: 1584
Reputation: 18751
I myself find your code a bit messy, so I can't really find the error.
I'll explain what I'd do from the beginning and maybe you can compare it with your code and get some ideas...
First I'd create these 2 classes:
class Response
List<Element> element;
class Element
String id;
String subtitle;
String name;
String subline;
String count;
Then I'd create a TypeAdapter
, and I think the easiest way is to create one for the Response
class. Something like:
public class ResponseDeserializer implements JsonDeserializer<Response> {
public Response deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) {
JsonElement elementField = json.getAsJsonObject().get("element");
List<Element> elementList = new ArrayList<>();
if (elementField.isJsonArray()) {
for (JsonElement item : elementField.getAsJsonArray()) {
elementList.add(ctx.deserialize(item, Element.class));
}
} else if (elementField.isJsonObject()) {
elementList.add(ctx.deserialize(elementField, Element.class));
}
Response response = new Response(elementList);
return response;
}
}
Then you just need to register the adapter with:
GsonBuilder builder = new GsonBuilder().registerTypeAdapter(Response.class, new ResponseDeserializer());
Gson gson = builder.create();
And eventually parse the JSON with:
Response response = gson.fromJson(data, Response.class);
Upvotes: 3