is there any easy way to to convert this json:
{
...,
"pictures": [
"url1",
"url2"
],
...
}
to
List<Picture> pictures
where Picture is:
class Picture{
String url;
}
It won't work as above, because I have an exception, saying
Expected BEGIN_OBJECT but was STRING
You need to implement a custom deserializer for this. Should be looking like this (I didn't try to execute, but that should give you the idea where to start, presumably you have a public constructor with String
argument in your Picture.class
)
private class PictureDeserializer implements JsonDeserializer<Picture> {
public Picture deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new Picture(json.getAsJsonPrimitive().getAsString());
}
}
It should be registered:
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Picture.class, new PictureDeserializer());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.