Gson calls returns empty objects over multiple classes

前端 未结 2 1542
一个人的身影
一个人的身影 2021-01-16 06:20

I\'m trying to serialize to serialize the json string I have included below.

 {
        \"mood\": {
            \"is_featured\": true,
            \"de         


        
2条回答
  •  执念已碎
    2021-01-16 06:29

    Root JSON object you provided has property mood - so you either have two options for deserialization to work properly:

    1. Wrap your Mood class inside another object like this:

      public class MoodWrapper { private Mood mood; }

      and change de-serialization code to

      MoodWrapper moodWrapper = new Gson().fromJson(result, MoodWrapper.class);

    2. Skip a root object when deserializing:

      final Gson gson = new Gson();
      JsonParser parser = new JsonParser();
      JsonObject rootObj = parser.parse(json).getAsJsonObject();
      Mood mood = gson.fromJson(rootObj.getAsJsonObject("mood"), Mood.class);
      

提交回复
热议问题