How to deserialize nested JSON array in Java with Gson? [closed]

╄→尐↘猪︶ㄣ 提交于 2020-04-17 18:58:26

问题


Something wrong in creating structure of Java Model Class for Deserializing this type of json

[
  {
    "name": "Chapter 1",
    "samples": [
      {
        "name": "Topic 1",
        "uri": "video link url 1",
        "extension": "mpd"
      },
      {
        "name": "Topic 2",
        "uri": "video link url 2",
        "extension": "mpd"
      }
    ]
  },
  {
    "name": "Chapter 2",
    "samples": [
      {
        "name": "Topic 1",
        "uri": "video link url 1",
        "extension": "mpd"
      }
    ]
  }
]

This is what i am trying

This is my custom model Class

public class PostSample {
    @SerializedName("name")
    private String mName;
    @SerializedName("uri")
    private String mUri;
    @SerializedName("extension")
    private String mExtension;

    public PostSample(String name, String uri, String extension) {
        mName = name;
        mUri = uri;
        mExtension = extension;
    }
}

This will be nested outer Java Model class which i am trying to create nested model.

public class SampleData {

    @SerializedName("name")
    private String mName; //<------- i have to make it List array
    @SerializedName("samples")
    private List<PostSample> mSamples;

    public SampleData(String name, List<PostSample> samples) {
        mName = name;
        mSamples = samples;
    }
}

For json deserialization using GSON

Gson gson = new Gson();
  String json = getIntent().getStringExtra("dataVideo");//Here it pass string of json of above structure

  SampleData[] samplesData = gson.fromJson(json, SampleData[].class);

i checked the similer question but not able to make the model and deserialize it.

来源:https://stackoverflow.com/questions/61256944/how-to-deserialize-nested-json-array-in-java-with-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!