Let's say I have a JSONObject like this example :
{ "name":"John",
"age":30,
"car":null,
contact": {
"telephone": "12345",
"extension": "34
}
}
and I would like to create a class in which I would write some methods to get certain values from JSONObjects with this format.
My first thought was to create a class (let's call it "Person") which extends JSONObject and add the methods I need, the problem is that I get the JSONObject as the return object of a method in a REST api, not from a JSONObject's class constructor, so I cannot create a constructor in Person using it's parent constructor nor cast that JSONObject to Person as I would have ClassCastException. What do you think is the best/proper way to approach this? What am I missing?
Another option would be creating Person class (no extends) with a JSONObject as a class variable and create a constructor with JSONObject as the only parameter. But that's seems not much "elegant" to me.
That's one of those issues that make me think I probably don't understand enough OOP yet as I have the impression there's an obvious way to get it done I can't see now haha
Thanks in advance!