Is it possible to analyze JSON data from a complex object to a custom object via API

advertisements

I have a web project with 2 Java Entities(Lets Say E1,E2) like how mybatis and VO works.

Object structure:

class E1{
   String a;
    .... n other data members
   E2 e2;
}
class E2{
   String b;
   .... n other data members
}

Is it possible to make a single class in Android project, i.e.

class E1 {
    String a;
    String b; //This Data member belongs to class E2
}

and parse it with the help of a framework (like Jackson) or I have to write a custom class for that?

My JSON Data will look like this:

{
"E1": {
    "a": "some data",
    .
    .
    .
    "E2": {
        "b": "some data",
        .
        .
        other data
        }
    }
}

Is there any API which can do this? I asked this because with my web Application its not just 2 Class but atleast 10 interconnected class and I am not Using them in my android app. So don't wanna replicate the same classes in android app.

Also if you can suggest any other possible way.


It would be a very bad design practice/approach, making things very difficult to debug, error prone and not future proof (think about it, what if you add to one of the 10 classes a field that conflict with another class' field?).

Anyway, if you still want to trick your way out of the correct approach that would be to have 10 classes, I am not aware of any lib that provides you with this feature. You could parse json ==> 10 Java Map, then merge the 10 Map through the Map::putAll method and finally pass the obtained Map that contains all the objects to Jackson.