1

the json string from server looks like below (Simplified)

{
   "p1":"v1",
   "c1":{
        "a1":"string1",
        "a2":"string2"
        }
}

or

{
   "p1":"v2",
   "c2":{
        "b1":"string3",
        "b2":"string4"
        }
}

is it a way to use jackson to deserialize the json string like that:

class P contains common response info(sign,status code,status msg......) class C1 and class C2 have their own specific business data

class P{
    String p1;
}
class C1 extend P{
    String a1;
    String a2;
}
class C2 extend P{
    String b1;
    String b2;
}

Question(Simplified):

{"p1":"v1","c1":{"a1":"string1","a2":"string2"}} to Class C1,

{"p1":"v2","c2":{"b1":"string3","b2":"string4"}} to Class C2

XGFan
  • 11
  • 4
  • So what is your question, you are getting any issue while de-sterilizing above string for C1/C2 class – Sachin Gupta Aug 08 '17 at 07:04
  • His question is (i guess) if it's possible to convert a json two more than one Object? Probably this helps? https://stackoverflow.com/questions/22680630/how-to-use-jackson-objectmapper-to-convert-to-pojo-for-multiple-data – Markus G. Aug 08 '17 at 07:05

1 Answers1

-1

Yes you can convert each object into specific class by using field that will indicate class type in provided base class like this one below

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME, 
  include = JsonTypeInfo.As.PROPERTY, 
  property = "type")
@JsonSubTypes({ 
  @Type(value = Car.class, name = "car"), 
  @Type(value = Truck.class, name = "truck") 
})
public class Vehicle {
       private String type; //where type car for Car.class and truck for Truck.class
    // fields, constructors, getters and setters
}
Sławomir Czaja
  • 283
  • 1
  • 7