Simple Outbound REST API with JSON Response using Apex in Salesforce

Simple Outbound REST API with JSON Response using Apex in Salesforce

Remote Site Settings:

Visualforce Page:
<apex:page controller=”RESTAPIJSONResponseController”>
            <apex:pageBlockButtons location=”bottom”>
                <apex:commandButton value=”Fetch” action=”{!fetchAPI}”/>
                <apex:pageBlockTable value=”{!listWrapper}” var=”obj”>
                    <apex:column value=”{!}” headerValue=”Id”/>
                    <apex:column value=”{!obj.login}” headerValue=”Login”/>
                    <apex:column value=”{!obj.url}” headerValue=”URL”/>
                    <apex:column value=”{!obj.description}” headerValue=”Description”/>

Apex Class:

public with sharing class RESTAPIJSONResponseController {

    public List < JSONWrapper > listWrapper {get;set;}
    public RESTAPIJSONResponseController() {
        listWrapper = new List < JSONWrapper >();
    public void fetchAPI() {
        HTTP h = new HTTP();
        HTTPRequest req = new HTTPRequest();
        HTTPResponse res = h.send(req);  
        JSONParser parser = JSON.createParser(res.getBody());
        listWrapper = (List < JSONWrapper >) JSON.deSerialize(res.getBody(), List < JSONWrapper >.class);
           If the response contains only one value instead list, then you can use the below code
           JSONWrapper obj = (JSONWrapper) JSON.deSerialize(res.getBody(), JSONWrapper.class); 
    public class JSONWrapper {
        public String login {get;set;}
        public String id {get;set;}
        public String url {get;set;}
        public String repos_url {get;set;}
        public String events_url {get;set;}
        public String hooks_url {get;set;}
        public String issues_url {get;set;}
        public String members_url {get;set;}
        public String public_members_url {get;set;}
        public String avatar_url {get;set;}
        public String description {get;set;}


Leave a Reply