简体   繁体   English

如何将Gson仅需的字段映射到Json到Model

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

i have some Json string 我有一些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;"
    }
}}    

How can I geg this model from this Json string by Gson 我怎样才能从Gson的这个Json字符串中得出这个模型

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

I do not wont create model with all fields and can I mapping needed for me fields from json to my model(even if they are chields) 我不会创建所有字段的模型,并且可以将我需要的字段从json映射到我的模型(即使它们是字段)

I suggest you implement JsonDeserializer for each class you want to populate with data from JSON. 我建议您为每个要用JSON数据填充的类实现JsonDeserializer Eg: 例如:

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

And the if you test it with provided json like this: 如果您使用提供的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());
    }
}

The output will be: 输出将是:

Widget:on,main_window,sun1

And this also provides good example of setter-getter solution for Gson. 这也为Gson提供了一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM