Home > Enterprise >  How can I display JSON data in a Salesforce Visualforce Page?
How can I display JSON data in a Salesforce Visualforce Page?

Time:05-26

EDIT : Here's what I was thinking for the callout :

public class APISocieteCallout {

String searchContent= //Content of the search
String apiKey= 'XXXXXXXXXXXXXXXXXXXXXX';
String requestEndpoint  = 'https://api.societe.com/pro/dev/societe/';
requestEndpoint  = 'search?nom=';
requestEndpoint  = '&token=' apiKey;
requestEndpoint  = '&format=json';

public static HttpResponse getSocieteInfo(){
Http http = new Http();
HttpRequest request = new HttpRequest();
requestEndpoint= 'https://api.societe.com/pro/dev/societe/';
request.setMethod('GET');


HttpResponse response = http.send(request);
System.debug(response.getStatusCode());

if(response.getStatusCode() == 200){
   Map<String,Object> result = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
   System.debug(result);
               
}

return response;

} }

Sorry, I am new to this and can't get the different documentations to help...

Thanks a lot, H.

CodePudding user response:

One way would be to create some helper class with these fields (public Integer n; public String siren; etc) and deserialize the JSON into proper apex objects. Or pass it directly as is to Visualforce as string variable and do something with it in JavaScript. Depends what you consider cleaner and what else you plan to do with this data. For just displaying on 1 VF page parsing properly is bit of overkill but if there are other things that could use it or JavaScript is not an option (say VF page rendered as pdf for example...). On the other hand if you'd be making a Lightning Web Component - no point wasting time parsing it,pass it to JS as is.

You don't have to create the helper wrapper class by hand. You could feed your JSON document to demo gif

  • Related