deserialize json field into plain string with gson

后端 未结 2 1506
野的像风
野的像风 2021-01-04 17:41

I am trying to deserialize a json object into a java bean. The main issue I am facing is that I\'d like to treat the field object of the json string as a plain

相关标签:
2条回答
  • 2021-01-04 18:10

    I don't know if your problem is solved. I ran into a similar question and here it is how I worked it out:

    JsonDeserializer allows you to make you own adapter to deserialize that **:

    class JavaBeanDeserializer implements JsonDeserializer<JavaBeanObject>() {
        public JavaBeanObject fromJson(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        // return JavaBeanObject built using your logic.
    }
    

    You've to register JavaBeanDeserializer to Gson object when building it:

    Gson gson = new GsonBuilder().registerTypeAdapter(JavaBeanObject.class, new JavaBeanDeserializer()).create();
    
    0 讨论(0)
  • 2021-01-04 18:11

    Just declare it as of type JsonObject

    class ExampleJsonModel {
        @SerializedName("type")
        public String type;
    
        @SerializedName("object")
        public JsonObject object;
    }
    
    0 讨论(0)
提交回复
热议问题