+ Start a Discussion
Diwakar G 7Diwakar G 7 

Display Json Result in Visualforce page

Hi,
 
{"totalSize":11,"done":true,"records":[{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqNGAU"},"Name":"F-0000306","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqmGAE"},"Name":"F-0000311","Project__c":"P1","Release__c":"aDv210000008OUTCA2","Username__c":"aaag@bbb.com","Useremail__c":"aaa.g@bbb.com"},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqDGAU"},"Name":"F-0000304","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqXGAU"},"Name":"F-0000308","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqSGAU"},"Name":"F-0000307","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqhGAE"},"Name":"F-0000310","Project__c":"P1","Release__c":"R1","Username__c":"aaag@bbb.com","Useremail__c":"aaa.g@bbb.com"},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004Eq3GAE"},"Name":"F-0000302","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004Eq8GAE"},"Name":"F-0000303","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqIGAU"},"Name":"F-0000305","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqcGAE"},"Name":"F-0000309","Project__c":"P1","Release__c":"R1","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqrGAE"},"Name":"F-0000312","Project__c":"P1","Release__c":"R2","Username__c":"aaa.g@ccc.com","Useremail__c":"aaa.g@bbb.com"}]}
I want display the above json result in below format in visualforce page. How can I acheive this?

Name     Username__c        Useremail__c        Project__c          Release__c



Thanks and Regards,
Diwakar G
Raj VakatiRaj Vakati
Use this code

 
<apex:page controller="MyController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!jsonRes.records}" var="res">
                <apex:column headerValue="Name" value="{!res.Name}"/>
                <apex:column headerValue="Project" value="{!res.Project}"/>
                <apex:column headerValue="Release" value="{!res.Release}"/>
                <apex:column headerValue="Username" value="{!res.Username}"/>
                <apex:column headerValue="Useremail" value="{!res.Useremail}"/>
                
            </apex:pageBlockTable>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
 
public class MyController {
    
    private final String jsinMessage='{"totalSize":11,"done":true,"records":[{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqNGAU"},"Name":"F-0000306","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqmGAE"},"Name":"F-0000311","Project__c":"P1","Release__c":"aDv210000008OUTCA2","Username__c":"aaag@bbb.com","Useremail__c":"aaa.g@bbb.com"},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqDGAU"},"Name":"F-0000304","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqXGAU"},"Name":"F-0000308","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqSGAU"},"Name":"F-0000307","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqhGAE"},"Name":"F-0000310","Project__c":"P1","Release__c":"R1","Username__c":"aaag@bbb.com","Useremail__c":"aaa.g@bbb.com"},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004Eq3GAE"},"Name":"F-0000302","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004Eq8GAE"},"Name":"F-0000303","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqIGAU"},"Name":"F-0000305","Project__c":"P1","Release__c":"R2","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqcGAE"},"Name":"F-0000309","Project__c":"P1","Release__c":"R1","Username__c":null,"Useremail__c":null},{"attributes":{"type":"User_Story__c","url":"/services/data/v44.0/sobjects/User_Story__c/aEX210000004EqrGAE"},"Name":"F-0000312","Project__c":"P1","Release__c":"R2","Username__c":"aaa.g@ccc.com","Useremail__c":"aaa.g@bbb.com"}]}';  
    public fromJSON jsonRes {get;set;}
    // jsonRes =fromJSON.parse(jsinMessage);
    
    public MyController() {
        jsonRes =  (fromJSON) System.JSON.deserialize(jsinMessage, fromJSON.class);
    }
    
    
    
    
    public class fromJSON{
        public Integer totalSize{get;set;}
        public boolean done{get;set;}
        public cls_records[] records{get;set;}
        /*
public static fromJSON parse(String json){
return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
}
*/
    }
    public class cls_records {
        public cls_attributes attributes{get;set;}
        public String Name{get;set;}//F-0000306
        public String Project{get;set;}	//P1
        public String Release{get;set;}	//R2
        public cls_Username Username{get;set;}
        public cls_Useremail Useremail{get;set;}
    }
    public  class cls_attributes {
        public String type{get;set;}	//User_Story__c
        public String url{get;set;}	///services/data/v44.0/sobjects/User_Story__c/aEX210000004EqNGAU
    }
    public class cls_Username {
    }
    public class cls_Useremail {
    }
}

 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Diwakar

It seems to be a JSON string of your custom object? If yes then you can deserialize the data into your object format and then use that object on your VF page.

For instance : User_Story__c userStory = JSON.deserialize(<JSON_STRING>,User_Story__c.class);

Cheers!!!
Diwakar G 7Diwakar G 7
Hi Raj,

Only name is showing.
User-added image
Thanks and Regards,
Diwakar G
Raj VakatiRaj Vakati
You need to replace __c with sapce in your all JSON .. other wise you have to use Normal JSON parsing not JSON to Apex 

 
String jsonInput = '{"item": 4, "change":  "40", "limit": 400}';
    
Map<String, Object> m =   (Map<String, Object>) JSON.deserializeUntyped(jsonInput);

Integer a =  (Integer)m.get('item');
System.debug('item:' + a); 

String c = (String)m.get('change');
System.debug('change:' + c);