问题
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