[英]JSON parse single string to object
is there any easy way to to convert this json: 有没有简单的方法来转换此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
) 应该看起来像这样(我没有尝试执行,但这应该让您知道从哪里开始,大概在
Picture.class
有一个带有String
参数的公共构造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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.