How to parse this JSON in APEX?

Louise01 :

With the code below I can access variables Name, Weight and Value, but not the variable AdvisoryModel. I guess this is because it is out of the AssetClassDistrib node.

How can I obtain the AdvisoryModel variable?

My code is as follows:

public class FromJSON_V1 {
public static string input ='{"AdvisoryModel": "","AdvisoryModelName": "","AnalysisDate": "2017-09-01","AssetClassDistrib": [{"Code": "5917","Name": "Equities - Emerging Markets","Value": 278.79749999999956,"Weight": 0.284105297215223}]}';
    public static void parse(){

        Map<String,Object> jsonParsed =(Map<String,Object> ) JSON.deserializeUntyped(input);
         System.debug(jsonParsed);
        List<Object> entriesArray =( List<Object> ) jsonParsed.get('AssetClassDistrib');

        for(Object inidividualEntries : entriesArray){
            Map<String,Object> ind = (Map<String,Object> )inidividualEntries;
            System.debug('Id = '+ ind.get('AdvisoryModel'));
            System.debug('Id = '+ ind.get('Name'));
            System.debug('Id = '+ ind.get('Weight'));
            System.debug('Id = '+ ind.get('Value'));
        }

    }

}
TechingCrewMatt :

The "AdvisoryModel" property did not have a value specified in the JSON. Also, it is noT a property of the objects in the array. It is a property in the outermost object.

Please try the code below, ad let me know if you're seeing the expected results:

public class FromJSON_V1 {
    public static string input ='{"AdvisoryModel": "test","AdvisoryModelName": "","AnalysisDate": "2017-09-01","AssetClassDistrib": [{"Code": "5917","Name": "Equities - Emerging Markets","Value": 278.79749999999956,"Weight": 0.284105297215223}]}';
    public static void parse(){
        Map<String,Object> jsonParsed =(Map<String,Object> ) JSON.deserializeUntyped(input);
        System.debug(jsonParsed);
        List<Object> entriesArray =( List<Object> ) jsonParsed.get('AssetClassDistrib');
        System.debug('AdvisoryModel before loop =' + jsonParsed.get('AdvisoryModel'));
        for(Object inidividualEntries : entriesArray){
            Map<String,Object> ind = (Map<String,Object> )inidividualEntries;
            System.debug('AdvisoryModel = ' + jsonParsed.get('AdvisoryModel'));
            System.debug('Name = '+ ind.get('Name'));
            System.debug('Weight = '+ ind.get('Weight'));
            System.debug('Value = '+ ind.get('Value'));
        }  
    }
}

With complex objects like this, it may be easier to deserialize into an Apex type instead of an un-typed map. If you are not sure what the response will be in the future, then a map is a good solution. If you're assuming that there will always be an array of objects with those properties, it might as well be a concrete object.

If you stick with the map, I would suggest some type checking to avoid run-time errors if the "AssetClassDistrib" property doesn't contain an array for some reason.

    Object assetClassDistrib = jsonParsed.get('AssetClassDistrib');
    List<Object> entriesArray =  assetClassDistrib instanceof List<Object> ? 
        (List<Object>)jsonParsed.get('AssetClassDistrib') : 
        new List<Object>();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=33179&siteId=1