Android – use dynamic arrays and objects to handle gson
•
Android
I get the response from the API, because if there is data, it is the return object, and when the data does not exist, it is the return empty array. I have a model for serializing this model. This is the model class
@SerializedName("local")
public Local _local = new Local();
public class Local{
@SerializedName("total")
public String _total;
@SerializedName("providers")
public ArrayList<ProvidersDetails> _providers = new ArrayList<>();
public class ProvidersDetails{
@SerializedName("id")
public String _id;
@SerializedName("image")
public String _image;
}
@SerializedName("admin")
public transient Admin admin = new Admin();
public class Admin{
@SerializedName("id")
public String _id;
@SerializedName("first_name")
public String _first_name;
@SerializedName("last_name")
public String _last_name;
}
@SerializedName("orgn")
public Organization _organization = new Organization();
public class Organization{
@SerializedName("name")
public String _ name;
}
}
This is part of the response I got from the API
"local":{
"providers":[
{
"id":"1180",
"image":"photo.png"
},
{
"id":"1180",
"image":"photo.png"
},
{
"id":"1180",
"image":"photo.png"
}
],
"admin":{
"id":"1180",
"first_name":"nqgnerq",
"last_name":"gnejbeqp",
},
"orgn":{
"name":"organization name"
}
}
This is another form I get when the data doesn't exist
"local":{
"total":0,
"providers":[
],
"admin":[
],
"orgn":{
"name":"organization name"
}
}
I checked many workarounds, but they all failed because I wanted to deal with it in my POJO class. If there is any solution, please make suggestions
resolvent:
You can use "jsondeserializer" for processing. Here is an example. Make the class providersdetails serializable
Create such a new class
public class ProvidersDetailsList {
public ArrayList<ProvidersDetails> getDetails() {
return providersDetails;
}
ArrayList<ProvidersDetails> providersDetails= new ArrayList<>();
}
Now write the deserializer
public class PhotoAlbumDeserializer implements JsonDeserializer {
@Override
public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
ProvidersDetailsList providersDetailsList= new ProvidersDetailsList ();
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject != null) {
JsonElement usersField = jsonElement.getAsJsonObject().get("providers");
if (usersField == null || usersField.isJsonNull() || usersField.isJsonPrimitive())
; // if is null or is a primitive type will return an empty result
else if (usersField.isJsonObject()) {
providersDetailsList.getDetails().add((ProvidersDetails) jsonDeserializationContext.deserialize(usersField, ProvidersDetails.class));
} else if (usersField.isJsonArray()) {
Type listOfUserType = new TypeToken<List<ProvidersDetails>>() {
}.getType();
providersDetailsList.getDetails().addAll((Collection<? extends ProvidersDetails>) jsonDeserializationContext.deserialize(usersField, listOfUserType));
}
}
return providersDetailsList;
}
}
Now call it that
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ProvidersDetailsList.class, new PhotoAlbumDeserializer());
Gson gson = gsonBuilder.create();
ArrayList<ProvidersDetails> providersDetails = gson.fromJson(jsonString, ProvidersDetailsList.class);
This is using gson https://stackoverflow.com/a/16591621/3111083 Do link
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码