6

I would like to make a http callout to import json and display the information in a visualforce page. The problem I'm having is that the json is nested and so I need to loop through the child nodes.

I have the following class derived using the json2apex application:

public class CHForm {

    public class FilingHistoryItem {
        public String DocumentDate;
        public String FormType;
        public String DocumentCategory;
        public String Document;
        public String DocumentDescription;
      }

    public FilingHistory FilingHistory;

    public class FilingHistory{
        public List<FilingHistoryItem> FilingHistoryItem;
      }
    public static CHForm parse(String json) {
        return (CHForm) System.JSON.deserialize(json, CHForm.class);
      }
}

In my controller class I can create a CHForm object (reponseForm) and deserialise the json into the reponseForm object using the following:

HttpResponse res = h.send(req);
String chFormJson = res.getBody();
responseForm  = CHForm.parse(chFormJson);

but how do I then display a list of all FilingHistoryItem in the visualforce page? Do I need to create a list object in my controller or is there a way of directly referencing the list from the visualforce page?

3

The direct reference, assuming responseForm is already a public property is:

<apex:pageBlockTable value="{!responseForm.FilingHistory.FilingHistoryItem}" var="item">

but it is probably cleaner to go via this:

public List<FilingHistoryItem> items {
    get {
        return responseForm.FilingHistory.FilingHistoryItem;
    }
}

referenced like this in the page:

<apex:pageBlockTable value="{!items}" var="item">
0

To display a List in Visualforce page:

in your controller declare a property as below:

 public List<FilingHistoryItem> FilingHistoryItem{get;set;}
 or
  public List<FilingHistoryItem> FilingHistoryItem{ get
                                                      {
                                                       //your custom code to return a list of filling history items
                                                       }
                                                       set;}

Then in your visualforce you can display the list as below:

    <apex:pageBlockTable value="{!FilingHistoryItem}" var="fht">
  <apex:column value="{!fht.DocumentDate}"/>
  </apex:pageBlockTable>

you can add columns to display values of FilingHistoryItem attributes as you like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.