3

I have this JSON Response, and I'm having trouble trying to figure out both the JSON.deserializeUntyped and JSON Parser. My ultimate goal is that I need to parse the JSON, then take each value and include it into a Detail Page for an object. From what I can tell I need to make a class for the objects in the outer array, and then another wrapper class for the items within those arrays? What I'm confused about is how to isolate each value and get it to the page I want.

JSON Response From HTTP Callout That I Need Help Parsing

{"ideal":[{"product":"4","percent":"25","dollarAmount":"11250"},{"product":"5","percent":"23","dollarAmount":"10350"},{"product":"3","percent":"20","dollarAmount":"9000"},{"product":"8","percent":"18","dollarAmount":"8100"},{"product":"7","percent":"15","dollarAmount":"6750"}],"idealFiltered":[{"product":"4","percent":"29","dollarAmount":"10500"},{"product":"5","percent":"26","dollarAmount":"9414"},{"product":"8","percent":"24","dollarAmount":"8690"},{"product":"7","percent":"21","dollarAmount":"7603"}],"InBudget":[{"product":"4","percent":"70","dollarAmount":"7000"},{"product":"8","percent":"30","dollarAmount":"3000"}]}

Apex Class minus the HTTP callout that I'm leaving out, but works

global class ApexCallout {
    id thisRecId;
    Recommendation_Request__c recParams;
    public String recResponseBody { get; set; }
    Recommendation_Response__c recResponse { get; set; }

    public MicFire(ApexPages.standardController sc){
        thisRecId = ApexPages.currentPage().getParameters().get('id');
        recParams = [SELECT Rest_API_Parms__c FROM Recommendation_Request__c WHERE Id = :thisRecId];
    }
            public class IdealResponse{
                public List<recResponseItems> IdealResponse;
            }

            public class recResponseItems {
                public string product;
                public integer percent;
                public double dollarAmount;
            }

            IdealResponse ideal = (IdealResponse)JSON.deserialize(recResponseBody, IdealResponse.class);

            public class idealFiltered {
                public List<idealFiltered> idealFiltered;
            }

            public class idealFilteredWrapper {
                public string product;
                public integer percent;
                public double dollarAmount;
            }

            public class inBudget {
                public List<inBudget> inBudget;
            }

            public class inBudgetWrapper {
                public string product;
                public integer percent;
                public double dollarAmount;
            }
  • Hi @stephen, could you find a solution for this? Do mark the answer if it worked for you. – MnZ Aug 12 '16 at 16:17
4

Using JSON2Apex (here), you can easily generate a class that you can use to deserialize your JSON.

JSON2Apex outputs code like this:

    public class Outer {

    public List<Ideal> ideal;
    public List<Ideal> idealFiltered;
    public List<Ideal> InBudget;

    public class Ideal {
        public String product;
        public String percent;
        public String dollarAmount;
    }

    public static Outer parse(String json) {
        return (Outer) System.JSON.deserialize(json, Outer.class);
    }
}

From here, you can iterate through the lists inside the main Object and extract the values as you need.

The key is the parse method. What you need to do is pass in your string like this:

Outer myParsedResult = Outer.parse(httpCalloutResponseString);

Then you can literally go:

for (Ideal idealItem : Outer.ideal){
    //do something here... maybe if a value is x, assign that value to your 
    //public property on your vf page etc...
}
  • I saw this site while researching before asking this question. What I'm more interested in understanding is what this code block is actually doing. And getting at least one example of how to extract the value like I need to do. I've seen this type of code block numerous times on here, but was hoping to get a more in depth answer if possible. – Stephen Jun 10 '15 at 2:37
  • 2
    Added a few extra details - hopefully this is helpful – Caspar Harmer Jun 10 '15 at 2:48
  • @Stephen are you familiar with serialization and deserialization? – Pittsburgh DBA Jun 10 '15 at 4:31
  • @PittsburghDBA a little bit, but having trouble isolating each value. Any extensive examples would be really helpful though, instead of small code blocks with partial answers. – Stephen Jun 10 '15 at 12:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.