function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NKrishnaNKrishna 

How to deserialize the external webservice output

Hi,

 

I am using external web service to fetch the users data in salesforce. In that, I can use the userId values to JSON serialization and pass the value to external webservice and I get the USerdata in a JSON fomat.

 

{"PrimaryUserInfo":{"createdTimestamp":"2012-12-05 00:00:00.0","updateTimestamp":"2012-12-05 00:00:00.0","userFirstName":"nkm","userMiddleName":"","userLastName":"Krishna","userStatus":"ACTIVE"},"additionalUserCode":[{"UserCode":"K7","name":"NKMT MADDY","title":""}, {"UserCode":"E3","name":"HOLE","title":""}], "associatedUserCode":[{"UserCode":"M7","name":"QWERT MADDY","title":""}, {"UserCode":"V3","name":"HOLEY KILLI","title":""}],"managedUserCode":[{"UserCode":"A1","name":"ADAM GIL","title":""},{"UserCode":"B4","name":"ADAM MORE","title":""},{"UserCode":"C4","name":"ADRO BATISTA","title":""},{"UserCode":"H4","name":"ALIS MICHAEL","title":""},{"UserCode":"V7","name":"ALKS LIDES","title":""},{"UserCode":"T3","name":"HOLEWA","title":""}]}

 Once I am using deserializing I can't fetch to objects, I can fetch only strings.

 

Here is my Salesforce classes.,

public class PrimaryUserInfo {    
    public String createdTimestamp;
    public String updateTimestamp;
    public String userFirstName;
    public String userMiddleName;
    public String userLastName;
    public String userStatus;
}  
public class AddUserInfo {
        
    public String RepCode;
    public String RepName;
    public String title;        
}
public class ViewUser{		
	public PrimaryUser 			PrimaryUserInfo;		
	public List<AddUserInfo>	additionalUserCode;
	public List<AddUserInfo>	associatedUserCode;
	public List<AddUserInfo>	managedUserCode;						
}

 So How to deserize into the JSON values to apex class objects like I need ViewUser Apex class objects??

 

Here is the code for deserilzing the JSON.,

System.JSONParser parser = JSON.createParser(aaa);
parser.nextToken(); // START OBJECT
parser.nextToken(); // name
parser.nextToken(); // primaryRep value
PrimaryUserInfo primaryUsers = (PrimaryUserInfo)parser.readValueAs(PrimaryUserInfo.class); 
system.debug('primaryUsers '+ primaryUsers);
parser.nextToken();

while (parser.nextToken() != JSONToken.END_OBJECT) {
	if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
		String text = parser.getText();
		parser.nextToken();
		RepCodeWrapper RepCodeRow = new RepCodeWrapper();
		if(text == 'managedUserCode') {
		} else if(text == 'Name') {
			RepCodeRow.RepName = parser.getText();
		} else if(text == 'UserCode') {
			RepCodeRow.RepCode = parser.getText();
		} else {
			System.debug(LoggingLevel.WARN, 'JsonClassifier consuming unrecognized property: '+text);
			//consumeObject(parser);
		}
	}
}

 

In that I can get PrimaryUserInfo Object. But I need to get AdditionalUserInfo, AssociatedUSerInfo and ManagedUserInfo Object values.

 

Please help me.

 

Thanks,

Krishna.

Abhinav GuptaAbhinav Gupta

Try using JSON.deserialize() or JSON.serialize() it can directly get the job done for you. More details here: http://www.tgerm.com/2011/10/winter12-jsonparser-serialize.html

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

There is an app of heruko platform 

 

json2apex.herokuapp.com

 

Just input your JSON you will Obtain the Deserialise Apex object straight away.

 

Thanks