APEX JSON Deserialize

I have a JSON string that has nested objects with dynamic names that change every time. For example:

{
    "Objects": {
        "dynamicName1": {
            "name": "test"
        },
        "dynamicName2": {
            "name": "test"
        }
    }
}

      

I was wondering how you can deserialize this line in APEX using wrapper classes?

I've tried this:

public class masterobj
{   public childobj Objects;    
}

public class childobj
{   public el dynamicName1;
    public el dynamicName2;     
}

public class el
{   public string name;
}

String s = '{"Objects":{"dynamicName1":{"name":"test"},"dynamicName2":{"name":"test"}}}';
masterobj mo = (masterobj)JSON.deserialize(s, masterobj.class);

      

which works well when you have declared dynamic variable names in the class for each nested object.

The problem and question is how can I make this work using a dynamic variable in a wrapper class. Since the names of the objects will be different as well as the number of objects, I cannot hard code the names as they are different every time.

Any ideas?

+3


source to share


2 answers


You won't be able to deserialize a structure like the data binding structure of a json parser, you need to use a streaming json parser to read it.



+3


source


Use a map:



public class masterobj
{   
    Map<String, el> Objects;
}

      

0


source







All Articles