i wanted to know about JSON apex calls, I have a "integrationhelperJSON" class who already to wrote integration class to store JSON values and to parse everything , right now I have a VF page - account searching and displaying the results , first i have to search in SFDC if found display it then if not found I have to call this JSON method and display those results here .

Is there any example of it to do it?

I just need to call that method from my class in else condition?

It is helpful if you throw some light on me on this .

Thanks in advance.

  • 3
    You would paste your code as well and point out specific line where you are facing issue.
    – Ashwani
    Jul 10 '15 at 10:11
  • What do you exactly want ..? Do you want to call one apex class of orgA from another class of orgB. Jul 10 '15 at 12:06

My Class looks like the below : public class JSONParserDemo {

//Considering the same JSON from previous post but only with two questions 
String jsonstring ='{"Questions": {"Question": [{"Q": "Which of the following is a responsibility of Supplier Management?","o1": "Development, negotiation and agreement of Operational Level Agreements (OLAs)","o2": "Development, negotiation and agreement of the Service Portfolio", "o3": "","a": "Development, negotiation and agreement of contracts","ua": "NA"},{ "Q": "Which of the following does the Availability Management process include? 1. Ensuring services are able to meet availability targets 2. Monitoring and reporting actual availability 3. Improvement activities, to ensure that services continue to meet or exceed their availability goals", "o1": "1 and 2 only","o2": "1 and 3 only","o3": "","a": "All the Above","ua": "NA"}]}}';
List<QuestionsWrapper> parsedquestions = new List<QuestionsWrapper>();

public List<QuestionsWrapper> getParsedquestions(){
       // Create a JSON Parser from String.
    JSONParser parser = JSON.createParser(jsonstring);
    while (parser.nextToken() != null) {
        // Check for JSON array starting.
        // START_ARRAY This token is returned when '[' is encountered.
        if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
            while (parser.nextToken() != null) {
                // Check for JSON object starting
                //  START_OBJECT This token is returned when '{' is encountered.
                if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
                    // Read entire invoice object, including its array of line items.
                    QuestionsWrapper qw = ( QuestionsWrapper)parser.readValueAs(QuestionsWrapper.class);
                    system.debug('Question' + qw.q);             

                    parsedquestions.add(qw);               }
     return parsedquestions;  

//Inner class of required JSON object type
public class QuestionsWrapper

{ public String Q{get;set;} public String o1{get;set;} public String o2{get;set;} public String o3{get;set;} public String a{get;set;} public String ua{get;set;} }


And My Page :

<apex:pageBlock id="pageblock">
    <apex:pageBlockSection title="JSON Parsed Values" >
<apex:pageBlockTable value="{!parsedquestions}" var="q" id="pageblocktable">
    <apex:column headerValue="Question" value="{!q.Q}" />
    <apex:column headerValue="Option1" value="{!q.o1}" />
    <apex:column headerValue="Option2" value="{!q.o2}" />
    <apex:column headerValue="Option3" value="{!q.o3}" />
    <apex:column headerValue="Answer" value="{!q.a}" />
    <apex:column headerValue="User Answer" value="{!q.ua}" />

This is very useful information shared by Sravan

And thanks for this post may be useful for others too. Thanks.

  • 1
    You've posted a link-only answer which often get voted for deletion. It's preferred that you provide details from the link giving credit to the author, particularly should the link ever disappear. If you will, please update your answer with more information.
    – crmprogdev
    Jul 11 '15 at 14:52

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.