Gson calls returns empty objects over multiple classes

前端 未结 2 1540
一个人的身影
一个人的身影 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);
      
    0 讨论(0)
  • 2021-01-16 06:33

    The top-level elements in the JSON string should be your object's properties, not the outer element "mood" which you have.

    0 讨论(0)
提交回复
热议问题