Wednesday 1 October 2014

How to Convert JSON Data to List Class in Salesforce

Problem

  Need to format JSON response and display the response values in PageBlockTable in visualforce page.

JSON String
  1. [
  2.     {
  3.         "label": "Winter '11",
  4.         "url": "/services/data/v20.0",
  5.         "version": "20.0"
  6.     },
  7.     {
  8.         "label": "Spring '11",
  9.         "url": "/services/data/v21.0",
  10.         "version": "21.0"
  11.     },
  12.     {
  13.         "label": "Summer '11",
  14.         "url": "/services/data/v22.0",
  15.         "version": "22.0"
  16.     },
  17.     {
  18.         "label": "Winter '12",
  19.         "url": "/services/data/v23.0",
  20.         "version": "23.0"
  21.     },
  22.     {
  23.         "label": "Spring '12",
  24.         "url": "/services/data/v24.0",
  25.         "version": "24.0"
  26.     },
  27.     {
  28.         "label": "Summer '12",
  29.         "url": "/services/data/v25.0",
  30.         "version": "25.0"
  31.     },
  32.     {
  33.         "label": "Winter '13",
  34.         "url": "/services/data/v26.0",
  35.         "version": "26.0"
  36.     },
  37.     {
  38.         "label": "Spring '13",
  39.         "url": "/services/data/v27.0",
  40.         "version": "27.0"
  41.     },
  42.     {
  43.         "label": "Summer '13",
  44.         "url": "/services/data/v28.0",
  45.         "version": "28.0"
  46.     },
  47.     {
  48.         "label": "Winter '14",
  49.         "url": "/services/data/v29.0",
  50.         "version": "29.0"
  51.     },
  52.     {
  53.         "label": "Spring '14",
  54.         "url": "/services/data/v30.0",
  55.         "version": "30.0"
  56.     },
  57.     {
  58.         "label": "Summer '14",
  59.         "url": "/services/data/v31.0",
  60.         "version": "31.0"
  61.     }
  62. ]
Before creating class, Copy your JSON Response and Paste the response here Json2Apex then create apex. You will get the apex code based on your JSON Response and you can alter it based on your requirement. I have altered the auto generated code for the above json response and the code is,

Apex Class

  1. global class SalesforceVersionInfo
  2. {
  3.     public List<SFInstance> sfInstances{get;set;}
  4.    
  5.     public SalesforceVersionInfo()
  6.     {
  7.         String jsonString = '[{\"label\":\"Winter \'11\",\"url\":\"/services/data/v20.0\",\"version\":\"20.0\"},{\"label\":\"Spring \'11\",\"url\":\"/services/data/v21.0\",\"version\":\"21.0\"},{\"label\":\"Summer \'11\",\"url\":\"/services/data/v22.0\",\"version\":\"22.0\"},{\"label\":\"Winter \'12\",\"url\":\"/services/data/v23.0\",\"version\":\"23.0\"},{\"label\":\"Spring\'12\",\"url\":\"/services/data/v24.0\",\"version\":\"24.0\"},{\"label\":\"Summer \'12\",\"url\":\"/services/data/v25.0\",\"version\":\"25.0\"},{\"label\":\"Winter\'13\",\"url\":\"/services/data/v26.0\",\"version\":\"26.0\"},{\"label\":\"Spring \'13\",\"url\":\"/services/data/v27.0\",\"version\":\"27.0\"},{\"label\":\"Summer\'13\",\"url\":\"/services/data/v28.0\",\"version\":\"28.0\"},{\"label\":\"Winter \'14\",\"url\":\"/services/data/v29.0\",\"version\":\"29.0\"},{\"label\":\"Spring\'14\",\"url\":\"/services/data/v30.0\",\"version\":\"30.0\"},{\"label\":\"Summer \'14\",\"url\":\"/services/data/v31.0\",\"version\":\"31.0\"}]';
  8.         sfInstances = (List<SFInstance>) System.JSON.deserialize(jsonString, List<SFInstance>.class);
  9.         sfInstances.sort();
  10.     }
  11.    
  12.     global class SFInstance implements Comparable
  13.     {
  14.         public String label{get;set;}
  15.         public String url{get;set;}
  16.         public String version{get;set;}
  17.        
  18.         public Integer compareTo(Object ObjToCompare)
  19.         {
  20.             return label.CompareTo(((SFInstance)ObjToCompare).label);
  21.         }
  22.     }
  23.    
  24. }

Visualforce Page

  1. <apex:page controller="SalesforceVersionInfo">
  2. <apex:form >
  3. <apex:pageBlock >
  4. <apex:pageBlockTable value="{!sfInstances}" var="sf">
  5. <apex:column headerValue="Label" value="{!sf.label}"/>
  6. <apex:column headerValue="URL" value="{!sf.url}"/>
  7. <apex:column headerValue="Version" value="{!sf.version}"/>
  8.  </apex:pageBlockTable>
  9. </apex:pageBlock>
  10. </apex:form>
  11. </apex:page>

Result



4 comments:

  1. Arun,

    What is the use of the CompareTo method here? Can you pl elaborate?

    ReplyDelete
    Replies
    1. Balaji,

      compareTo method used for sorting purpose. In the above code, I have sorted the wrapper list using label value.

      You can find out more on this from the below links,

      https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_comparable.htm
      http://www.infallibletechie.com/2013/08/comparable-interface-in-salesforce.html

      Delete
  2. Hi Guys, I have all latest dumps of salesforce certification (Summer ‘17) if anyone wants you can mail me at
    sfdcconsultant25@gmail.com

    These are original questions from the certification exam and very useful to pass the exam.
    Above 90% questions come from it
    I have all latest dumps of following exams

    Salesforce Administrator (ADM 201)
    Salesforce Sales Cloud Consultant (CON 201)
    Salesforce Service Cloud Consultant
    Platform Developer I
    App Builder and App builder transition exam

    Good Day!

    ReplyDelete
  3. I read that Post and got it fine and informative دمج ملفات pdf

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...