How to display values of List<Map<String, Object>> by using <apex:pageBlockTable>?

Viewed 19

I'm trying to display values of a List<Map<String, Object>> in a Visualforce page by using apex:pageBlockTable.

I'm stuck on what to put in apex:outputField value like below.

Is there any way that this can be made to work? The number of Map<String, Object> is variable.

apex:

String theJsonString = '[{"id":1, "name":"Abc_SS", "description":"Abc", "address":"Abc"}, {"id":2, "name":"sales", "description":"sales", "address":"Abc"}]';
Object theJsonObject = JSON.deserializeUntyped(theJsonString);
List<Object> theJsonList = (List<Object>) theJsonObject;
List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();

for (Object obj : theJsonList) {
    theJsonMapList.add((Map<String, Object>) obj);
}

visualforce page:

<apex:pageBlockTable value = "{!theJsonMapList}" var="al">
    <apex:column headerValue="id">
        <apex:outputField value=??>
    </apex:column>
    <apex:column headerValue="name">
        <apex:outputField value=??>
    </apex:column>
    <apex:column headerValue="discription">
        <apex:outputField value=??>
    </apex:column>
    <apex:column headerValue="address">
        <apex:outputField value=??>
    </apex:column>
</apex:pageBlockTable>
1 Answers

You can't use outputField at all because it's "magic". It works only with real database sobjects (Account, Contact, custom objects...) because it then can read what's the field type and how to properly display it (lookup - make a link. Date - format. etc)

Your data is unrelated, bunch of generic Objects so you're limited to outputText etc.

This should work nicely:

public class Stack73773910 {

    public List<Map<String, Object>> getData(){
        String theJsonString = '[{"id":1, "name":"Abc_SS", "description":"Abc", "address":"Abc"}, {"id":2, "name":"sales", "description":"sales", "address":"Abc"}]';
        Object theJsonObject = JSON.deserializeUntyped(theJsonString);
        List<Object> theJsonList = (List<Object>) theJsonObject;
        List<Map<String, Object>> theJsonMapList = new List<Map<String, Object>>();
        
        for (Object obj : theJsonList) {
            theJsonMapList.add((Map<String, Object>) obj);
        }
        return theJsonMapList;
    }
}
<apex:page controller="Stack73773910">
    <apex:pageBlock>
    <apex:pageBlockTable value = "{!data}" var="al">
        <apex:column headerValue="id">
            <apex:outputText value="{!al['id']}"/>
        </apex:column>
        <apex:column headerValue="name">
            <apex:outputText value="{!al['name']}"/>
        </apex:column>
        
        <apex:column headerValue="description" value="{!al['description']}"/> <!-- you can cheat if you don't need special output like number / date / checkbox formatting -->
        <apex:column headerValue="address" value="{!al['address']}"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

enter image description here

In the long run... as your JSON grows more complex and maybe you start using it in Apex for more things, not just displaying it... You'll grow angry with all the casting to Date , Boolean, String, Decimal you'll have to do to access the data. There are tools to help make you a helper class to hold this data as a proper object, not a stupid Map<String, Object>.

Have a look at https://json2apex.herokuapp.com/. It will let you have a List<MyWrapper> - parsing it should be similar complexity to what you have now but then in Visualforce you'll be back to old, simple {!al.name}

Related