Tuesday, September 20, 2022

[FIXED] How to create a Model class having map as one of the variables

Issue

I am trying to create a model class for something like this

{
    "id": null,
    "name": "TestCompany",
    "domainName": "comany.domain",
    "status": 0,
    "brandAttributes": {
        "contact_person_name": "HS",
        "website_url": "company@gmail.com",
        "address": "Bengaluru",
        "contact_person_email": "hs@gmail.com"
    }
}

Solution

You can use nested class or 2 class to archive the Model

class ModelDTO{
    private String id;
    private String name;
    private String domainName;
    private long status;
    private BrandAttributes brandAttributes;
    
    class BrandAttributes{
        private String contact_person_name;
        private String website_url;
        private String address;
        private String contact_person_email;
    }
}

or

class ModelDTO{
    private String id;
    private String name;
    private String domainName;
    private long status;
    private BrandAttributes brandAttributes;
}
    class BrandAttributes{
        private String contact_person_name;
        private String website_url;
        private String address;
        private String contact_person_email;
    }


Answered By - Lokeshwar G
Answer Checked By - Pedro (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.