我有一些Json字符串
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
如何通过Gson
从这个Json字符串中对此模型进行gegpublic class widget{
private String debug;
private String windowName; //name from widget->window->name
private String imageName; //name from widget->image->name
}
我不会创建包含所有字段的模型,并且我可以将json中的字段映射到我的模型(即使它们是chields)
答案 0 :(得分:0)
我建议您为要使用JSON数据填充的每个类实现print
。 E.g:
JsonDeserializer
如果你用提供的json测试它,就像这样:
package jsonsmartmap;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
public class WidgetMapper implements JsonDeserializer<WidgetMapper.Widget>
{
@Override
public Widget deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException
{
JsonObject json = (JsonObject) je;
JsonObject jsonWidget = json.get("widget").getAsJsonObject();
Widget ret = new Widget();
ret.setDebug(jsonWidget.get("debug").getAsString());
ret.setWindowName(jsonWidget.get("window").getAsJsonObject().get("name").getAsString());
ret.setImageName(jsonWidget.get("image").getAsJsonObject().get("name").getAsString());
return ret;
}
class Widget
{
private String debug;
private String windowName; //name from widget->window->name
private String imageName; //name from widget->image->name
public void setDebug(String debug)
{ this.debug = debug; }
public void setWindowName(String windowName)
{ this.windowName = windowName; }
public void setImageName(String imageName)
{ this.imageName = imageName; }
public String getDebug()
{ return this.debug; }
public String getWindowName()
{ return this.windowName; }
public String getImageName()
{ return this.imageName; }
@Override
public String toString()
{
return "Widget:"+getDebug()+","+getWindowName()+","+getImageName();
}
}
}
输出将是:
package jsonsmartmap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Main {
public static void main(String[] args) {
String inputJsonString = "{\"widget\": {\n" +
" \"debug\": \"on\",\n" +
" \"window\": {\n" +
" \"title\": \"Sample Konfabulator Widget\",\n" +
" \"name\": \"main_window\",\n" +
" \"width\": 500,\n" +
" \"height\": 500\n" +
" },\n" +
" \"image\": { \n" +
" \"src\": \"Images/Sun.png\",\n" +
" \"name\": \"sun1\",\n" +
" \"hOffset\": 250,\n" +
" \"vOffset\": 250,\n" +
" \"alignment\": \"center\"\n" +
" },\n" +
" \"text\": {\n" +
" \"data\": \"Click Here\",\n" +
" \"size\": 36,\n" +
" \"style\": \"bold\",\n" +
" \"name\": \"text1\",\n" +
" \"hOffset\": 250,\n" +
" \"vOffset\": 100,\n" +
" \"alignment\": \"center\",\n" +
" \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"\n" +
" }\n" +
"}}";
Gson gson = (new GsonBuilder())
.registerTypeAdapter(WidgetMapper.Widget.class, new WidgetMapper())
.create();
WidgetMapper.Widget widget = gson.fromJson(inputJsonString, WidgetMapper.Widget.class);
System.out.println(widget.toString());
}
}
这也为Gson提供了setter-getter解决方案的好例子。