I need to get json response and parse the response into the object(Without using http callout).I need to have application(class) which can get response from other callback url and parse that response into objects.
[SalesForce] How to get Json response without rest api and parse the response in objects
Related Solutions
As sfdcfox suggested in a comment, you can use json2apex to generate the necessary code from a JSON sample. The app is a little out of date in that test methods should now be in a different class, so I generated the code and separated out the test method:
UserAgreements Class
public class UserAgreements {
public class DisplayUserInfo {
public String company;
public String fullNameOrEmail;
}
public List<UserAgreementList> userAgreementList;
public class UserAgreementList {
public String displayDate;
public DisplayUserInfo displayUserInfo;
public Boolean esign;
public String agreementId;
public String latestVersionId;
public String name;
public String status;
}
public static UserAgreements parse(String json) {
return (UserAgreements) System.JSON.deserialize(json, UserAgreements.class);
}
}
UserAgreementsTest Class
@isTest
public class UserAgreementsTest {
static testMethod void testParse() {
String json = '{'+
' \"userAgreementList\": ['+
' {'+
' \"displayDate\": \"2015-04-02T09:32:23-07:00\",'+
' \"displayUserInfo\": {'+
' \"company\": \"TEST\",'+
' \"fullNameOrEmail\": \"Tim 1234\"'+
' },'+
' \"esign\": true,'+
' \"agreementId\": \"AGREEMENT ID\",'+
' \"latestVersionId\": \"VERSION ID\",'+
' \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
' \"status\": \"OUT_FOR_SIGNATURE\"'+
' },'+
' {'+
' \"displayDate\": \"2015-04-02T03:32:57-07:00\",'+
' \"displayUserInfo\": {'+
' \"company\": \"TEST\",'+
' \"fullNameOrEmail\": \"Tim 1234\"'+
' },'+
' \"esign\": true,'+
' \"agreementId\": \"AGREEMENT ID\",'+
' \"latestVersionId\": \"VERSION ID\",'+
' \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
' \"status\": \"OUT_FOR_SIGNATURE\"'+
' },'+
' {'+
' \"displayDate\": \"2015-03-31T09:15:11-07:00\",'+
' \"displayUserInfo\": {'+
' \"company\": \"TEST\",'+
' \"fullNameOrEmail\": \"Tim 1234\"'+
' },'+
' \"esign\": true,'+
' \"agreementId\": \"AGREEMENT ID\",'+
' \"latestVersionId\": \"VERSION ID\",'+
' \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
' \"status\": \"RECALLED\"'+
' }'+
' ]'+
'}';
UserAgreements obj = UserAgreements.parse(json);
System.assert(obj != null);
}
}
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...
}
Best Answer
I'm not sure I understand your question. By "without calling out" and "can get response from other callback url" do you mean that you want to publish an endpoint that will accept JSON that is posted to it? If so you can create an @RestResource Apex class with a @HttpPost method that can hanle most of the work - including deserialization from a JSON String to Apex objects - for you.
But if you have the JSON string available and are just asking how to parse it, there are lots of tools to help you parse it into Apex objects. In the JSON class:
Which of these JSON methods you choose depends on how fixed (known up front) the format of the JSON is. I've listed the methods in order of "best for fixed" though to "best for not fixed".
PS
From your comments below it sounds like you want to setup a REST endpoint in Salesforce that your "call centre website" can invoke. (The process will have to include authentication.) A @RestResource Apex class works well for this and would look something like this:
where ... is up to you: zero or more primitive values, simple Apex classes, SObjects, arrays of any of these. The framework automatically deserializes the JSON based on the parameter list.
To provide an example, this JSON:
would be automatically deserialized by this code:
You mention "it should be dynamic" but I am not sure what you mean by that.