Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I deserialize array of different objects with GSON

Tags:

java

json

gson

I receive JSON like this

[
  {
    "albums" : [
      {"id":"0", "name":"name"},
      {"id":"1", "name":"name"}
    ],
    "name":"name"
  },
  {
    "tracks" : [
      {"id":"0", "name":"name", "duration":"3:30"},
      {"id":"1", "name":"name", "duration":"2:40"}
    ],
    "name":"name"
  },
  {
    "playlists" : [
      {"id":"0", "name":"name", "tracksCount":"3"},
      {"id":"1", "name":"name", "tracksCount":"40"}
    ],
    "name":"name"
  }
]

Of course I implemented classes Track, Album and Playlist which contains all the fields and classes

Tracks {
  String name;
  List<Track> tracks;
}
Albums {
  String name;
  List<Album> albums;
}
Playlists {
  String name;
  List<Playlist> playlists;
}

I'm trying to deserialize it with:

Gson gson = new Gson();
JsonResponse[] rez = gson.fromJson(str, JsonResponse[].class);

where JsonResponse is

class JsonResponse {
  Albums albums;
  Tracks tracks;
  Playlists playlists;
}

But I get the error:

11-20 19:24:55.210: E/AndroidRuntime(5432): FATAL EXCEPTION: main 11-20 19:24:55.210: E/AndroidRuntime(5432): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.Gson.fromJson(Gson.java:803) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.Gson.fromJson(Gson.java:768) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.Gson.fromJson(Gson.java:717) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.Gson.fromJson(Gson.java:689) 11-20 19:24:55.210: 11-20 19:24:55.210: E/AndroidRuntime(5432): at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43) 11-20 19:24:55.210: E/AndroidRuntime(5432): at android.os.Handler.handleCallback(Handler.java:587) 11-20 19:24:55.210: E/AndroidRuntime(5432): at android.os.Handler.dispatchMessage(Handler.java:92) 11-20 19:24:55.210: E/AndroidRuntime(5432): at android.os.Looper.loop(Looper.java:130) 11-20 19:24:55.210: E/AndroidRuntime(5432): at android.app.ActivityThread.main(ActivityThread.java:3687) 11-20 19:24:55.210: E/AndroidRuntime(5432): at java.lang.reflect.Method.invokeNative(Native Method) 11-20 19:24:55.210: E/AndroidRuntime(5432): at java.lang.reflect.Method.invoke(Method.java:507) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 11-20 19:24:55.210: E/AndroidRuntime(5432): at dalvik.system.NativeStart.main(Native Method) 11-20 19:24:55.210: E/AndroidRuntime(5432): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374) 11-20 19:24:55.210: E/AndroidRuntime(5432): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165) 11-20 19:24:55.210: E/AndroidRuntime(5432): ... 21 more

P.S. Some Items in json response might be missed. For example

[
  {
    "albums" : [
      {"id":"0", "name":"name"},
      {"id":"1", "name":"name"}
    ],
    "name":"name"
  }
 ]

So what is the problem and how can I deserialize this JSON?

BTW I can't change JSON. So I need a code that parse it as it is.

like image 605
oleg.semen Avatar asked Mar 22 '23 14:03

oleg.semen


1 Answers

Thanks everybody for help! Here is my solution.

public class JsonResponse {
    public static class Item {
        String name;
    }

    public static class Tracks extends Item {
        List<Track> tracks;
    }

    public static class Albums extends Item {
        List<Album> albums;
    }

    public static class Playlists extends Item {
        List<Playlist> playlists;
    }

    public static class JsonResponseDeserialize implements JsonDeserializer<List<? extends Item>> {
        Gson gson = new Gson();
        @Override
        public List<? extends Item> deserialize(JsonElement el, Type type, JsonDeserializationContext context) throws JsonParseException {
            List<Item> ls = new ArrayList<Item>();
            JsonArray jarr = el.getAsJsonArray();
            for (JsonElement e : jarr) {
                Item i;
                if (e.getAsJsonObject().get("tracks") != null){
                    i = gson.lsomJson(e, Tracks.class);
                    if (i != null) {
                        ls.add(i);
                        continue;
                    }
                }
                if (e.getAsJsonObject().get("albums") != null){
                    i = gson.lsomJson(e, Albums.class);
                    if (i != null) {
                        ls.add(i);
                        continue;
                    }
                }
                if (e.getAsJsonObject().get("playlists") != null){
                    i = gson.lsomJson(e, Playlists.class);
                    if (i != null) {
                        ls.add(i);
                        continue;
                    }
                }
            }
            return ls;
        }
    }

    private Tracks _tracks;
    private Albums _albums;
    private Playlists _playlists;
}

Deserialization:

private static List<? extends Item> getDatalsomJson(String jsonString) {
    Type type = new TypeToken<List<? extends JsonResponse.Item>>(){}.getType();
    GsonBuilder gb = new GsonBuilder();
    gb.registerTypeAdapter(type, new JsonResponse.JsonResponseDeserialize());
    Gson gson = gb.create();
    List<? extends Item> ls = gson.lsomJson(jsonString, type);
    return ls;
}
like image 198
oleg.semen Avatar answered Apr 06 '23 02:04

oleg.semen