function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bharath kumar 52bharath kumar 52 

parsing json response and displaying it on a vf page

Hi All,

I am working on a PoC where i am consuming data from an external servcie and displaying it on a VF page.
Can someone let me know where i am going wrong and what needs to be done to display the data on the VF page.
public class TickerData {
	
	public list<Prices> pricelist{get;set;}
    private static String endpoint='https://koinex.in/api/ticker';
    
    public list<Prices> getmakeTickerData(){
		
		
		pricelist = new list<Prices>();
        Http http= new Http();
        HttpRequest request= new HttpRequest();
        request.setendpoint(endpoint);
        request.setMethod('GET');
        HttpResponse response = http.send(request); //response contains json body
        
        if(response.getStatusCode()==200){
            system.debug('Response'+response.getBody());
            //JSON2Apex js=JSON2Apex.parse(response.getBody());
            JSONParser parser = JSON.createParser (response.getBody()); 
   			while (parser.nextToken()!=null) { 
                if (parser.getCurrentToken()== JSONToken.START_ARRAY) {    
                    while (parser.nextToken()!= null) { 
                        
                        //
			if(response.getstatusCode() == 200 && response.getbody() != null){

				pricelist=(List<Prices>)json.deserialize(response.getbody(),List<Prices>.class);

			}//
                        /*if (parser.getCurrentToken()== JSONToken.START_OBJECT) { 
                            JSON2Apex the = (JSON2Apex) parser.readValueAs (JSON2Apex.class); 
                        }*/
                  }
            }
			
			
            //Map<String,Object> results=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
            /*List<Object> prices=(List<Object>)results.get('prices');
            
            for(Object o:prices){
                System.debug('Prices :'+o);
                
            }*/
        	}
        
    	}
        return pricelist;
	}


	public class Prices {
		public String BTC{get;set;}
		public String ETH{get;set;}
		public String XRP{get;set;}
		public String BCH{get;set;}
		public String LTC{get;set;}
		public String NEO{get;set;}
		public Double MIOTA{get;set;}
		public String TRX{get;set;}
		public String OMG{get;set;}
		public String AE{get;set;}
		public String ZRX{get;set;}
		public String GAS{get;set;}
		public String BAT{get;set;}
		public String GNT{get;set;}
		public String REQ{get;set;}
		public String AION{get;set;}
		public String NCASH{get;set;}
		public String XLM{get;set;}
	}
	
    public class JSON2Apex {
		public Prices prices;
		public Stats stats;
	}
    
	public Prices prices;
	public Stats stats;

	public class ETH {
		public String currency_full_form;
		public String currency_short_form;
		public String per_change;
		public Double last_traded_price;
		public String lowest_ask;
		public String highest_bid;
		public String min_24hrs;
		public String max_24hrs;
		public Integer vol_24hrs;
	}

	public class Stats {
		public ETH ETH;
		public ETH BTC;
		public ETH LTC;
		public ETH XRP;
		public ETH BCH;
		public ETH OMG;
		public ETH REQ;
		public ETH ZRX;
		public ETH GNT;
		public ETH BAT;
		public ETH AE;
		public ETH TRX;
		public ETH XLM;
		public ETH NEO;
		public ETH GAS;
		public ETH NCASH;
		public ETH AION;
	}

}
 
VF Page :

<apex:page controller="TickerData">
<apex:form>

<apex:pageBlock >

<apex:pageBlockTable value="{!makeTickerData}" var="wrap" width="100%">
<apex:column headerValue="BTC" value="{!wrap.BTC}"/>
<apex:column headerValue="ETH" value="{!wrap.ETH}"/>
</apex:pageBlockTable>

</apex:pageBlock>
    
</apex:form>
</apex:page>

 
Best Answer chosen by bharath kumar 52
Om PrakashOm Prakash
Hello Bharath,
I have modified your method getmakeTickerData()  which will parse the data correctly.
Please note there are no list of array for prices is returned in json from given url, thats why your parsing logic is not working.
JSON response seems simple so directly deserialized for class TickerData.
public list<Prices> getmakeTickerData(){
        pricelist = new list<Prices>();
        Http http= new Http();
        HttpRequest request= new HttpRequest();
        request.setendpoint(endpoint);
        request.setMethod('GET');
        HttpResponse response = http.send(request); //response contains json body
        
        if(response.getstatusCode() == 200 && response.getbody() != null){
           TickerData objTicketData  = (TickerData) System.JSON.deserialize(response.getbody(), TickerData.class);
           if(objTicketData != null && objTicketData.prices != null){
                pricelist.add(objTicketData.prices);
           }
        }
        return pricelist;
    }

Simply replaced your method 's code with above code and let me know if still you are not able to get data on vf page.

All Answers

MagulanDuraipandianMagulanDuraipandian
Hi,
Check debug log whether the data is returned back from the service.

Double check the column names BTC, ETC, etc are of the same case in the service too. 

Sample code - http://www.infallibletechie.com/2017/08/simple-outbound-rest-api-with-json.html
Om PrakashOm Prakash
Hello Bharath,
I have modified your method getmakeTickerData()  which will parse the data correctly.
Please note there are no list of array for prices is returned in json from given url, thats why your parsing logic is not working.
JSON response seems simple so directly deserialized for class TickerData.
public list<Prices> getmakeTickerData(){
        pricelist = new list<Prices>();
        Http http= new Http();
        HttpRequest request= new HttpRequest();
        request.setendpoint(endpoint);
        request.setMethod('GET');
        HttpResponse response = http.send(request); //response contains json body
        
        if(response.getstatusCode() == 200 && response.getbody() != null){
           TickerData objTicketData  = (TickerData) System.JSON.deserialize(response.getbody(), TickerData.class);
           if(objTicketData != null && objTicketData.prices != null){
                pricelist.add(objTicketData.prices);
           }
        }
        return pricelist;
    }

Simply replaced your method 's code with above code and let me know if still you are not able to get data on vf page.
This was selected as the best answer
bharath kumar 52bharath kumar 52
Thanks Om it's working as expected