簡體   English   中英

如何將Gson僅需的字段映射到Json到Model

[英]How can I mapping Json to Model by Gson only needed fields

我有一些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字符串中得出這個模型

public class widget{
private String debug;
private String windowName; //name from widget->window->name
private String imageName;  //name from widget->image->name
}

我不會創建所有字段的模型,並且可以將我需要的字段從json映射到我的模型(即使它們是字段)

我建議您為每個要用JSON數據填充的類實現JsonDeserializer 例如:

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();
        }
    }
}

如果您使用提供的json進行測試,例如:

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());
    }
}

輸出將是:

Widget:on,main_window,sun1

這也為Gson提供了一個很好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM