Problem
Need
to format JSON response and display the response values in PageBlockTable in
visualforce page.
JSON String
- [
- {
- "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"
- }
- ]
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
- global class SalesforceVersionInfo
- {
- public List<SFInstance> sfInstances{get;set;}
- public SalesforceVersionInfo()
- {
- 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\"}]';
- sfInstances = (List<SFInstance>) System.JSON.deserialize(jsonString, List<SFInstance>.class);
- sfInstances.sort();
- }
- global class SFInstance implements Comparable
- {
- public String label{get;set;}
- public String url{get;set;}
- public String version{get;set;}
- public Integer compareTo(Object ObjToCompare)
- {
- return label.CompareTo(((SFInstance)ObjToCompare).label);
- }
- }
- }
Visualforce Page
- <apex:page controller="SalesforceVersionInfo">
- <apex:form >
- <apex:pageBlock >
- <apex:pageBlockTable value="{!sfInstances}" var="sf">
- <apex:column headerValue="Label" value="{!sf.label}"/>
- <apex:column headerValue="URL" value="{!sf.url}"/>
- <apex:column headerValue="Version" value="{!sf.version}"/>
- </apex:pageBlockTable>
- </apex:pageBlock>
- </apex:form>
- </apex:page>
Result
Arun,
ReplyDeleteWhat is the use of the CompareTo method here? Can you pl elaborate?
Balaji,
DeletecompareTo 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
Hi Guys, I have all latest dumps of salesforce certification (Summer ‘17) if anyone wants you can mail me at
ReplyDeletesfdcconsultant25@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!
I read that Post and got it fine and informative دمج ملفات pdf
ReplyDelete