2

I am having difficulty understanding how to take a parsed JSON list and saving it into an APEX list. My end goal is to have the Username and associated PriorityNumber listed out on a VF page.

I have read the related questions here and I just can't seem to understand the why behind what I am doing. If you look to the bottom of my class, when I try to loop through myParsedResult, I get an error saying that "Variable does not exist: JSON2Apex".

I have tried deserialization, the wrapper classes, directly putting javascript into my VFP, and many variations of the Salesforce JSON page. If anyone could help exaplining how to save a parsed string into a list, it would be greatly appreciated.

public class WSController {
    public List<String> un {get;set;}
    public List<String> pn {get;set;}   
    public List<String> dmutc {get;set;}   

    String jsonStr = '{"DateModifiedUtc":"2016-09-07T20:12:47.1519119Z",' + 
'"AgentInfoList":[{"Username":"Agent00@gmail.com","PriorityNumber":0}, ' +
'{"Username":"Agent01@gmail.com","PriorityNumber":0},{"Username":"Agent02@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent03@gmail.com","PriorityNumber":0},{"Username":"Agent04@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent05@gmail.com","PriorityNumber":0},{"Username":"Agent06@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent07@gmail.com","PriorityNumber":0},{"Username":"Agent08@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent09@gmail.com","PriorityNumber":0},{"Username":"Agent10@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent11@gmail.com","PriorityNumber":0},{"Username":"Agent00@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent01@gmail.com","PriorityNumber":1},{"Username":"Agent02@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent03@gmail.com","PriorityNumber":1},{"Username":"Agent04@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent05@gmail.com","PriorityNumber":1},{"Username":"Agent06@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent07@gmail.com","PriorityNumber":1},{"Username":"Agent08@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent09@gmail.com","PriorityNumber":1},{"Username":"Agent10@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent11@gmail.com","PriorityNumber":1},{"Username":"Agent00@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent01@gmail.com","PriorityNumber":2},{"Username":"Agent02@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent03@gmail.com","PriorityNumber":2},{"Username":"Agent04@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent05@gmail.com","PriorityNumber":2},{"Username":"Agent06@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent07@gmail.com","PriorityNumber":2},{"Username":"Agent08@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent09@gmail.com","PriorityNumber":2},{"Username":"Agent10@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent11@gmail.com","PriorityNumber":2}]}';


// Parse entire JSON response.    
    public List <String> WSController() { 
        //Returns the token that the parser currently points to
        parser = JSON.createParser(jsonStr);
        if (parser.nextToken() != null) 
        {
            while ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Username'))
            {
                fieldName = parser.getText();
                //returns the value of the next token
                parser.nextToken();
                un.add(parser.getText());
                system.debug('this is the Username list: ' + un);
            }
            while ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'PriorityNumber'))
            {
                fieldName = parser.getText();
                //returns the value of the next token
                parser.nextToken();
                pn.add(parser.getText());
                system.debug('this is the Priority Number list: ' + pn);
            }
            while ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'DateModifiedUtc'))
            {
                fieldName = parser.getText();
                //returns the value of the next token
                parser.nextToken();
                dmutc.add(parser.getText());
                system.debug('this is the Date Modified: ' + dmutc);
            }
        }
        return pn;       
    }

    //wrapper class
    public class JSON2Apex 
    {
        public String DateModifiedUtc;
        public List<AgentInfoList> AgentInfoList;
    }
    public class AgentInfoList 
   {
        public String Username;
        public Integer PriorityNumber;
    }
    public static JSON2Apex parse(String json) 
    {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }

    JSON2Apex myParsedResult = JSON2Apex.parse(jsonStr);

    public void myMethod(){
        for (AgentInfoList agentItem : myParsedResult){
            un = agentItem.Username;
            pn = agentItem.PriorityNumber;
        }
    }

}

In addition, as you can see, I am saving the username and priority number lists in the WSController and then again in the wrapper class. I was following this example but I feel like I should be able to write this without the wrapper class.

4

"myMethod" didn't work because of compilation errors. Here's what it really should look like:

  for (AgentInfoList agentItem : myParsedResult.AgentInfoList){
        un.add(agentItem.Username);
        pn.add(agentItem.PriorityNumber);
  }

But that's overkill; AgentInfoList already has the data you need, so if you'd simply started off with that as a public variable, like this:

public class WSController {
    public JSON2Apex info { get; set; }

    String jsonStr = '{"DateModifiedUtc":"2016-09-07T20:12:47.1519119Z",' + 
'"AgentInfoList":[{"Username":"Agent00@gmail.com","PriorityNumber":0}, ' +
'{"Username":"Agent01@gmail.com","PriorityNumber":0},{"Username":"Agent02@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent03@gmail.com","PriorityNumber":0},{"Username":"Agent04@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent05@gmail.com","PriorityNumber":0},{"Username":"Agent06@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent07@gmail.com","PriorityNumber":0},{"Username":"Agent08@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent09@gmail.com","PriorityNumber":0},{"Username":"Agent10@gmail.com","PriorityNumber":0},' +
'{"Username":"Agent11@gmail.com","PriorityNumber":0},{"Username":"Agent00@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent01@gmail.com","PriorityNumber":1},{"Username":"Agent02@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent03@gmail.com","PriorityNumber":1},{"Username":"Agent04@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent05@gmail.com","PriorityNumber":1},{"Username":"Agent06@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent07@gmail.com","PriorityNumber":1},{"Username":"Agent08@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent09@gmail.com","PriorityNumber":1},{"Username":"Agent10@gmail.com","PriorityNumber":1},' +
'{"Username":"Agent11@gmail.com","PriorityNumber":1},{"Username":"Agent00@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent01@gmail.com","PriorityNumber":2},{"Username":"Agent02@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent03@gmail.com","PriorityNumber":2},{"Username":"Agent04@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent05@gmail.com","PriorityNumber":2},{"Username":"Agent06@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent07@gmail.com","PriorityNumber":2},{"Username":"Agent08@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent09@gmail.com","PriorityNumber":2},{"Username":"Agent10@gmail.com","PriorityNumber":2},' +
'{"Username":"Agent11@gmail.com","PriorityNumber":2}]}';

    //wrapper class
    public class JSON2Apex 
    {
        public String DateModifiedUtc { get; set; }
        public List<AgentInfoList> AgentInfoList { get; set; }
    }
    public class AgentInfoList 
   {
        public String Username { get; set; }
        public Integer PriorityNumber { get; set; }
    }
    public static JSON2Apex parse(String json) 
    {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
    public WSController() {
        info = (JSON2Apex)parse(jsonStr);
    }
}

You can then use that data in a page like this:

Date Modified: {!info.DateModifiedUtc}
<br />
<apex:repeat value="{!info.AgentInfoList}" var="AgentInfo">
    {!AgentInfo.Username} has priority {!AgentInfo.priorityNumber}.<br />
</apex:repeat>
  • Thank makes sense. I thought it was a bit over kill to be putting it in a list twice. So I am just saving the parsed string into 'info' and calling info on the VFP. But when I do this I am getting the error message--> "Unknown property 'WSController.JSON2Apex.DateModifiedUtc' " and if I take that out, I get error--> "Unknown property 'WSController.JSON2Apex.AgentInfoList'". It seems like it does not understand JSON2Apex. – Olivia Sep 9 '16 at 19:23
  • @Olivia classic mistake (I did it, too). You need to have public getters and setters; I've updated my code to show you what that would look like. – sfdcfox Sep 9 '16 at 19:30
  • hum...it doesn't like the getter and setter. I tried putting is outside of the JSON2Apex class too but no avail. error message --> "unexpected token: '}'" – Olivia Sep 9 '16 at 19:36
  • @Olivia It was missing a semi-colon. That was just a typo. I'll update. I just tested this in my sandbox to validate. – sfdcfox Sep 9 '16 at 20:33
  • thank you! I got it up and running. I appreciate all of your help :) – Olivia Sep 9 '16 at 20:51

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.