0

I have a JSON that I get as a webservice and deserialize it into classes.

The classes are :

 public class DataStructure{
      public String Name;
        public String Code;
        public String Description;
        public String Token;
        public Results Result;
 }




public class Results {
        public String Id;
        public String Date;
        public String Time;
        public List<Custom_ObjectA> A;
        public List<Custom_ObjectB> B;
        public List<Custom_ObjectC> C;

    }



public class Custom_ObjectA  {
        public String IU;
        public String name;
        public String mcv;
        public String nhg;
    }

public class Custom_ObjectB  {
        public String IU;
        public String name;
        public String mcv;
        public String nhg;
    }

public class Custom_ObjectC  {
        public String IU;
        public String name;
        public String mcv;
        public String nhg;
    }


 DataStructure response = (DataStructure)JSON.deserialize(JSON, DataStructure.class);

Inside the class DataStructure I have a Name variable - This Name is one of the types of the lists inside Result class.

That means that the name will be Custom_ObjectA , Custom_ObjectB Or Custom_ObjectC

I would like to check the size of the list that I got in the Results class. (Those 3 are just an example.. I have like 24 different lists).

I want to do something like :

response.result.'response.Name'.size()

how can I achieve that? (I would like not to go over all of them in a loop)

  • Consider just making the Result have one massive list of List<Object>, which would actually be a list of all the custom classes you are parsing. Then you could JSON.deserializeUntyped() each of them and get their values based on the Map<String, Object> format – Brian Miller May 1 at 13:33
  • I agree that Maps seem a better approach here. You could have a Map based on type for your A, B and C lists in Results; "Map<Type, List<Object>> ABCs", accessing these via ABCs."List<Object> theAs = result.ABCs.get(Custom_ObjectA.class);" - the only downside; you will need to cast when you access the entries from this list. – Phil W May 1 at 13:54
  • BTW, as you probably found out already, Apex does not support method-level reflection (and barely supports any sort of reflection at all - just the ability to instantiate an object of a given class, using the Type functionality or in JSON deserialization) AFAIK. – Phil W May 1 at 13:58
  • @PhilW Combined with an Apex property accessor, Map<Type...> or Map<String...> would work, cast and String/Type conversion could be all encapsulated. – identigral May 12 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.