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.