[SalesForce] REST API calls from salesforce and parse the JSON to display on the vf page

I have to make REST API call from salesforce to a HTTP URL GET method to get the JSON response parse it and display it on the vf page.

My Questions:

  1. From the class below how can I check the status code of the response?
  2. How to map the returned response to the Parser class which I generated from JSON2APEX using the sample JSON?
  3. How to do the mappings on the returned JSON to the account and corporation objects in salesforce?
  4. Can I display the info on vf page without saving or mapping to the corresponding objects in salesforce?

Integration Class:

public class MySQLIntegrationHelper{
public String getResult{get;set;}
//public String Response {get; set;}

public PageReference submit() {
    return null;

public String getData()
    HttpRequest req= new HttpRequest();
    Http http = new Http();
    String url = 'http://mysql.com/buyout/account/1234';
    String username = 'test';
    String password = 'testpw';
    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'BASIC ';
    System.debug( 'authorizationHeader : ' + authorizationHeader );
    req.setHeader('Authorization', authorizationHeader);
    try {
        HttpResponse res = new Http.send(req);

    } catch (System.CalloutException e) {
       System.debug('The callout failed with this message:'+e); 
    return null;  

JSON Parser:

public class MySQLJSONParser {

public class Corporations {
    public Integer id;
    public String listingid;
    public String corporation_id;
    public String location_id;
    public String title;

public Account Account;
public List<Corporations> Corporations;

public class Account {
    public Integer id;
    public String InternalAccountId;
    public String account_id;
    public String name;
    public String status;
    public String bid_type;

public static MySQLJSONParser parse(String json) {
    return (MySQLJSONParser) System.JSON.deserialize(json, MySQLJSONParser.class);

Best Answer

Here is an outline for 1/2/3 assuming you want to insert the data. If it's an update you'll need to query the existing versions of the objects:

HTTPResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
    Corporation__c corps = new Corporation__c[] {};
    MySQLJSONParser p = MySQLJSONParser.parse(res.getBody());
    for (MySQLJSONParser.Corporation c : p.Corporations) {
        corps.add(new Corporation__c(
                Title__c = c.title,
    insert corps;
} else {
    throw new MyException('HTTP callout failed:'
            + '\nendPoint=' + req.getEndPoint()
            + '\nstatusCode=' + res.getStatusCode()
            + '\nstatus=' + res.getStatus()
            + '\nbody=' + res.getBody()

On 4, yes you can present objects returned by the parse directly in your page if you want to: Visualforce can present Apex object data but you have to do more work such as outputting your own labels.