0

JSON Response

{
    "subscriptions": {
        "abc": "suspanded",
        "def": "ACTIVE",
        "ghi": "ACTIVE",
        "jkl": "ACTIVE"
    },
    "firstName": "testuser",
    "lastName": "test",
    "exostarUPN": "xyz@upn",
    "exostarEMAIL": "jentest.org",
    "a/c": "ACTIVE"
}

Apex wrapper class

public class fromJSON{
    public cls_subscriptions subscriptions;
    public String firstName;    
    public String exostarUPN;   
    public String exostarEMAIL; 
    public String a/c;  
    class cls_subscriptions {
        public String abc;  
        public String def;  
        public String ghi;  
        public String jkl;  
    }
}

how can I make cls_subscriptions dynamic coz cls_subscriptions class can hold value more then the variables I have decaled

1 Answer 1

0

In Apex you cannot add new properties (or new method) to an instance of a class at runtime, so once you defined some properties in a class, you can use only them.

Anyway if subscriptions can hold a mutable set of properties of the same type (string in your example), you could declare a Map<String, String> in your outer class, instead of relying on an inner class:

public class fromJSON {
    public Map<String, String> subscriptions;
    public String firstName;    
    public String exostarUPN;   
    public String exostarEMAIL; 
    public String a_c;
}

By the way, I changed a/c to a_c since / is not a valid character for a property's name. If it's the real name of a JSON property, you should replace it with the valid one before deserializing it:

String jsonString = '{ "subscriptions": { "abc": "suspanded", "def": "ACTIVE", "ghi": "ACTIVE", "jkl": "ACTIVE" }, "firstName": "testuser", "lastName": "test", "exostarUPN": "xyz@upn", "exostarEMAIL": "jentest.org", "a/c": "ACTIVE" } ';
fromJSON jsonData = (fromJSON) JSON.deserialize(jsonString.replace('"a/c"', '"a_c"'), fromJSON.class);
System.debug(jsonData);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .