1

An api provides different result in json everytime I hit. How can I generate a new model class everytime according to json response? Is it possible to create dynamic pojo class according dynamic json response everytime??

on 1st hit -

{
    "Banners": [
        {
            "banner_id": "30",
            "banner_image": ""
        }
    ],
    "success": "success",
    "message": "Banners"
}

on 2nd hit

{
    "Banners": [
        {
            "banner_desc": "returns between paragraphs",
            "banner_type": "regular",
            "banner_size": 100,
        }
    ],
    "success": "success",
    "message": "Banners"
}

on 3rd hit -

{
    "user_id": "4",
    "name": "",
    "email": "k@gmail.com",
    "mobile": "",
    "image": "",
    "success": "success",
    "message": "Registered"
}
  • Does this answer your question? [Creating classes dynamically with Java](https://stackoverflow.com/questions/2320404/creating-classes-dynamically-with-java) – dbl Oct 05 '20 at 08:10
  • FYI [POJO](https://en.wikipedia.org/wiki/Plain_old_Java_object) - Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification. So the short answer for POJOs is NO ;) – dbl Oct 05 '20 at 08:15
  • 3
    Why did you want to do this? How did you access the dynamical class? A better way is to deserialize the JSON response to a `Map` for further processing. – LHCHIN Oct 05 '20 at 08:49
  • key value pair in response will change everytime so even I cannot create Model class and add to hashmap..... I guess Hashmap will be good – Sarang Shende Oct 05 '20 at 08:52

0 Answers0