Parsing Http Response (JSON Format) using Apex

In Order to Parse JSON Response in Apex one must use JSONParse Class

  1. Initiate JSONParser as parser
  2. Pass res.getBody() or JSON body as parameter to Json.CreateParser()
  3. And parser.nextToken() is responsible for getting next Token/node of JSON body

here is example of Apex Class parsing JSON body

JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
    if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
        String fieldName = parser.getText();
        parser.nextToken();
        if (fieldName == 'ContactId') {
            //accesstoken = parser.getText();
            system.debug('contactId Text() => ' + parser.getText());
        } else if (fieldName == 'CompanyName') {
            //expiresIn = parser.getIntegerValue();
            system.debug('COmpany Text() => ' + parser.getText());
        } else if (fieldName == 'Country') {
            //expiresIn = parser.getIntegerValue();
            system.debug('COmpany Text() => ' + parser.getText());
        } else if (fieldName == 'LastName') {
            //expiresIn = parser.getIntegerValue();
            system.debug('COmpany Text() => ' + parser.getText());
        } else if (fieldName == 'City') {
            //expiresIn = parser.getIntegerValue();
            system.debug('COmpany Text() => ' + parser.getText());
        } else if (fieldName == 'State') {
            //expiresIn = parser.getIntegerValue();
            system.debug('COmpany Text() => ' + parser.getText());
        } else if (fieldName == 'FirstName') {
            //expiresIn = parser.getIntegerValue();
            system.debug('COmpany Text() => ' + parser.getText());
        }
    }
}

In System.debug() parser.getText() is used for getting attributes values fo JSON nodes, depending on datatype use parser.getIntegerValue(), etc.,

For more information on Apex JSON click here