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":"[email protected]","PriorityNumber":0}, ' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","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.

1 Answer 1

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":"[email protected]","PriorityNumber":0}, ' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":0},' +
'{"Username":"[email protected]","PriorityNumber":0},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":1},' +
'{"Username":"[email protected]","PriorityNumber":1},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","PriorityNumber":2},{"Username":"[email protected]","PriorityNumber":2},' +
'{"Username":"[email protected]","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>
5
  • 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
    Commented Sep 9, 2016 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
    Commented Sep 9, 2016 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
    Commented Sep 9, 2016 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
    Commented Sep 9, 2016 at 20:33
  • thank you! I got it up and running. I appreciate all of your help :)
    – Olivia
    Commented Sep 9, 2016 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .