This post is in continuation of my previous post regarding JSON Parsing.

In this post, what I am going to share is-

  • How to parse a little more complex JSON containing array. 
  • Once the JSON data is parsed, how to display the same information in Visual Force Page.
Check the below link to understand what I am trying to achieve at the end of this post
So let’s start with creating the Visual Force Page
<apex:page controller="StudentDetailController">
<apex:form >
<apex:pageBlock Title="JSON Parser Example">
<apex:commandButton value="Parse JSON Data" action="{!parseJSONData}"
reRender="studentData" />
<br />
<apex:pageBlock id="studentData">
First Name: <apex:inputText value="{!firstName}" />
Last Name: <apex:inputText value="{!lastName}" />

<apex:pageBlockSection title="Language Known:" collapsible="false">
<apex:selectList value="{!selectedLanguage}" multiselect="false"
<apex:selectOptions value="{!languageOptions}"/>
Now the controller-

public class StudentDetailController{  
public String firstName{get;set;}
public String lastName{get;set;}
public String selectedLanguage{get;set;}
private StudentDetailWrapper m;
public List<SelectOption> languageOptions;

public void parseJSONData(){
m = new ParseStudentData().parseData();

public void parseJSONData(String jsonMessage){
m = new ParseStudentData().parseData(jsonMessage);

public List<SelectOption> getLanguageOptions(){
if(null == languageOptions){
languageOptions = new List<SelectOption>();
return languageOptions;

private void doAllInitialization(){

private void initializeStudentName(){
this.firstName = m.firstName;
this.lastName = m.lastName;

private void initializeLanguageOptions(List<String> language){
languageOptions = new List<SelectOption>();
for(String aLanguage : language){
languageOptions.add(new SelectOption(aLanguage, aLanguage));

Now the class which will do the JSON Parsing-

public class ParseStudentData {
private StudentDetailWrapper m;

public StudentDetailWrapper parseData(String jsonMessage){
return doParsing(jsonMessage);

public StudentDetailWrapper parseData(){
String jsonMessage = '{"firstName" : "Sudipta", "lastName" : "Deb","language": ["Hindi", "German", "English", "Bengali"]}';
return doParsing(jsonMessage);

private StudentDetailWrapper doParsing(String jsonMessage){
CustomLogging.logMessage('StudentDetailController', 'parseJSONData', 'Starting of parseJSONData()', CustomLogging.INFO);
m = (StudentDetailWrapper) System.JSON.deserialize(jsonMessage, StudentDetailWrapper.class);
}catch(Exception ex){
String message = 'Error: ' + ex.getMessage() + '; Stack Trace:' + ex.getStackTraceString();
CustomLogging.logMessage('ParseStudentData', 'parseJSONData', message, CustomLogging.ERROR);
return m;

Now StudentDetailWrapper Class which will hold the student data-

public class StudentDetailWrapper {

public String firstName;
public List<String> language;
public String lastName;

And finally the test class – very important:

private class TestStudentDetailController {

private static void setEnableCustomLoggingFlag(Boolean value){
insert(new Decision_Object__c(Name='EnableCustomLogging', Value__c=value));

static testMethod void testJsonParseData() {
String jsonMessage = '{"firstName" : "Paromita", "lastName" : "Banerjee","language": ["German", "English", "Bengali"]}';

StudentDetailController studentDetailController = new StudentDetailController();


Note: If you are thinking what is the reason for this method setEnableCustomLoggingFlag(),please refer to my earlier post regarding Custom Logging in Salesforce. Link: Custom Logging in Salesforce

Now when we will open the Visual Force Page, it will look like-

But once you click the “Parse JSON Data” it is going to parse the JSON Data in background and populate the same in Visual Force Page like below-

That’s all. I am currently working on more complex examples. Will share very soon.
Any feedback is always welcome. Thanks