I'm trying to deserialize a nested JSON string and store the values so that I can insert them into a visualforce page. Something like this tutorial here.
However, I'm getting the error
"System.JSONException: No content to map to Object due to end of input".
I believe this is due to the fact that the JSON string I want is nested 1 level (see below)
{"study":{"moleculeDescription":"RONTAXYZMAB","protocolTitle":"IADM","protocolNumber":"AA00836","studyName":"Interferon alpha in SLE Phase II LCM Option","therapeuticArea":"INFLAMMATORY,AUTOIMMNE","startDate":"2001-12-25","endDate":"2003-12-22"}}
In my visualforce page I'd like to create a pageblock table and call the values of the Study class. How can I correctly modify my extension class (below) to get the values in the second level of JSON string?
public with sharing class restExt {
private string result;
private string studyJson;
public ApexPages.StandardController stdCtrl {get; set;}
public restExt(ApexPages.StandardController std) {
//constructor
stdCtrl=std;
this.studyJson = retrieve();
}
public String getStudyJSON(){
return this.studyJson;
}
public List<Study> getStudies(){
List<String> studyList = new List<String>();
//Parse JSON response
List<Study> result =(List<Study>)JSON.deserialize(this.studyJson , List<Study>.class);
return result;
}
private String retrieve() {
//call the REST API here
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http h = new Http();
String username = 'XYZ';
String password = 'XYZ';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setClientCertificateName('Name_CN');
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Api-Key', '7123458403');
req.setTimeout(120000);
req.setMethod('GET');
req.setEndpoint('https://apidev-example.com/study');
String result = '';
try{
//Make a request
res = h.send(req);
result = res.getBody();
}
catch(System.CalloutException e){
result = res.toString();
}
return result;
}
//Study Object
public class Study {
public String moleculeDescription {get;}
public String protocolTitle {get;}
public String protocolNumber {get;}
public String studyName {get;}
public String therapeuticArea {get;}
public String startDate {get;}
public String endDate {get;}
}
}
Here is my JSON2APEX output
public class JSON2Apex {
public class Study {
public String moleculeDescription;
public String protocolTitle;
public String protocolNumber;
public String studyName;
public String therapeuticArea;
public String startDate;
public String endDate;
}
public Study study;
public static JSON2Apex parse(String json) {
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}
}