1

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);
    }

}
2
  • Use json2apex to generate your wrapper class and it will also output your deserialisation method: json2apex.herokuapp.com Commented Feb 12, 2015 at 23:12
  • @Bachovski Thank you for your answer. I'm a beginner, can you please tell me how to use JSON2APEX to get what I need? Please see my updated question
    – rwegner7
    Commented Feb 12, 2015 at 23:21

2 Answers 2

1

Your output is a single class according to the json I see and not a list.

 List<Study> result =(List<Study>)JSON.deserialize(this.studyJson , List<Study>.class);

You shoud expect either a class or turn json output into alist of study objects by adding []

1

ANSWER: I didn't end up using JSON2Apex, but this below method worked for me. My JSON string was nested one level deep, so I used the parser.nextToken() method to get to the level I needed and add the values into a list.

List<StudyWrapper> Study = new List<StudyWrapper>();

public List<StudyWrapper> getStudy(){
        JSONParser parser = JSON.createParser(this.studyJson);
        while(parser.nextToken() != null){
                while(parser.nextToken()!= null){
                    if(parser.getCurrentToken()==JSONToken.START_OBJECT){
                        StudyWrapper s = (StudyWrapper)parser.readValueAs(StudyWrapper.class);
                        Study.add(s);
                    }
                }
            }
        return Study;
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .