Issue with parse json to java object using gson

后端 未结 2 492
旧巷少年郎
旧巷少年郎 2021-01-12 21:36

I want to parse json data into java object using google-gson.

Here is an example of the data I want to parse:

  {\"isBean\":{\"userName\":\"test\",\         


        
相关标签:
2条回答
  • 2021-01-12 22:12

    The problem is solved With the help of Stackoverflow Answer.

    Create gson object:

    Gson gson = new GsonBuilder().registerTypeAdapter(IsBean.class, new InterfaceAdapter<IsBean>()).create();
    
    0 讨论(0)
  • 2021-01-12 22:30

    Without seeing the code of CommunicationObject, I can't say for sure BUT I am guessing with confidence that the class has a field of type IsBean in which you use it to hold the User. If so, the problem is that GSON scans fields of the object obj of the class CommunicationObject and based on the field definition, the value of the field (which is typed IsBean) will have to be created BUT since the type is an interface, it can't instance object for that field.

    Another words, as JSON field does not specifies the object type, GSON must relies on the defined type of the field to create the instance for the field value which can't be done if the type is an interface.

    So, consider changing the type of that field if that make sense to you. OR look into creating InstanceCreator of IsBean (doute that it is logically possible).

    See also: Deserializing an abstract class in Gson

    Hope this helps.

    0 讨论(0)
提交回复
热议问题