Problem
Need to format the JSON response and display the response values in PageBlockTable and sort the Pageblock table based on the label value from the given response.
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
The label column from the below table is sorted and here is the screenshot,
No comments:
Post a Comment