0

I'm building an API to show data in a Visualforce page. After I created the classes and the callout it responses with a JSON string. For now I use a mock JSON string to test my deserialize function.

Sadly it does not work, it says in the log files that the object contains attributes which are still null.

My classes:

public class MortgageContract {
    //@SerializedName('Number')
    public String Id {get;set;}
    public String Description {get;set;}
    public Collateral Collateral {get;set;}
    public String EffectiveDate {get;set;}
    public String InitialAmount {get;set;}
    public String ProRestoAmount {get;set;}
    public List<Borrower> Borrowers {get;set;}
    public List<LoanPart> LoanParts {get;set;}
}

public class Collateral{
    public Address Address {get;set;}
    public String MarketValue {get;set;}
}

public class Address{
    public String StreetAddress {get;set;}
    public String ZipCode {get;set;}
    public String City {get;set;}
}

public class Borrower{
    public String Name {get;set;}
}

public class LoanPart{
    public String Loanpartid {get;set;}
    public String Description {get;set;}
    public String ProRestoAmount {get;set;}
    public String InterestRate {get;set;}
    public String InterestType {get;set;}
    public String TermAmount {get;set;}
    public String TermPayment {get;set;}
    public String TimePayment {get;set;}
    public Boolean NHG {get;set;}
    public String EffectiveDate {get;set;}
    public String EndDateLoanpart {get;set;}
    public String DurationLoanpart {get;set;}
    public String EndDateFixedInterestRatePeriod {get;set;}
}

My JSON response:

{
"mortgageContract": {
    "id": "H0000000",
    "number": "H0000000",
    "productcode": "COM",
    "description": "Combinatiehypotheek",
    "collateral": {
        "address": {
            "streetAddress": "A VD NEERSTR 9",
            "zipCode": "5702 CJ",
            "city": "HELMOND"
        },
        "marketValue": "167500.00"
    },
    "effectiveDate": "2007-06-01",
    "initialAmount": "209375.00",
    "redemptionAmount": "35142.96",
    "proRestoAmount": "174232.04",
    "redemptionPercentage": "16.78",
    "loanToValue": "106.00",
    "prepayments": {
        "redemptionAmountActual": "0.00",
        "redemptionFreeAmountActual": "0.00",
        "redemptionFreeAmount": "20937.50",
        "redemptionFreePercentage": "10.00"
    },
    "borrowers": [{
        "name": "T.G.V.T. Sick - Bos"
    }],
    "lifeInsurances": [{
        "number": "2134437"
    }],
    "loanParts": [{
            "loanpartid": "H0000000-1",
            "loanpartnumber": "1",
            "productcode": "USH",
            "description": "Spaarhypotheek",
            "proRestoAmount": "50000.00",
            "interestRate": "5.30",
            "interestType": "Variabele rente",
            "termAmount": "0.00",
            "termType": "Rente",
            "termPayment": "Maandelijks",
            "timePayment": "Achteraf",
            "termComposition": {
                "termCompositionType": "Alleen rente",
                "termInterestAmount": "0.00"
            },
            "NHG": false,
            "redemptionDescription": "Bij deze hypotheekvorm betaalt u alleen rente, en lost u tijdens de looptijd niet direct af. Pas aan het einde van de looptijd lost u de hele hypotheek in één keer af. U spaart hiervoor in de gekoppelde levensverzekering.",
            "effectiveDate": "2007-06-01",
            "endDateLoanpart": "2037-06-01",
            "durationLoanpart": "30",
            "endDateFixedInterestRatePeriod": "2017-06-01",
            "prepayment": {
                "isAllowed": false,
                "producttype": "Non-ALA-PR"
            }
        },
        {
            "loanpartid": "H0000000-2",
            "loanpartnumber": "2",
            "productcode": "ALV",
            "description": "Aflossingsvrije hypotheek",
            "proRestoAmount": "67076.73",
            "interestRate": "2.34",
            "interestType": "Vaste rente",
            "termAmount": "130.80",
            "termType": "Rente",
            "termPayment": "Maandelijks",
            "timePayment": "Achteraf",
            "termComposition": {
                "termCompositionType": "Alleen rente",
                "termInterestAmount": "130.80"
            },
            "NHG": false,
            "redemptionDescription": "Bij deze hypotheekvorm lost u tijdens de looptijd niets af. U betaalt de openstaande lening terug aan het einde van de looptijd. Of op het moment dat u verhuist. Hiervoor gebruikt u eigen geld. Maar u kunt ook de opbrengst uit de verkoop van uw woning gebruiken.",
            "effectiveDate": "2007-06-01",
            "endDateLoanpart": "2037-06-01",
            "durationLoanpart": "30",
            "endDateFixedInterestRatePeriod": "2019-06-01",
            "prepayment": {
                "isAllowed": false,
                "producttype": "NON-ALA"
            }
        }
    ]
}
}

My deserialize function:

public static MortgageContract deserialize(httpResponse res){
    System.debug('Begin deserializing: + '+ res.getBody());
    return (MortgageContract)JSON.deserialize(res.getBody(),MortgageContract.class);
}

Please help me out!

  • Can you post exact error message? – kurunve Jun 20 '18 at 11:39
  • There is no error message, it just doesn't work.. Debug gives: 13:15:06:072 USER_DEBUG [120]|DEBUG|MortgageContract:[Borrowers=null, Collateral=null, Description=null, EffectiveDate=null, Id=null, InitialAmount=null, LoanParts=null, ProRestoAmount=null] – Jeroen Joris Jun 20 '18 at 11:44
1

Problem is that your JSON is not really a MortrageContract entity but rather a wrapper above it, so it is actually an object with property named mortgageContract and type MortgageContract .

To make it working, you need to define yet another class, for example, MCWrapper:

public class MCWrapper {
    MortgageContract mortgageContract{get;set;}
}

And in this case, just use similar code:

public static MortgageContract deserialize(httpResponse res){
    System.debug('Begin deserializing: '+ res.getBody());
    MortgageContractWrapper mc_wrapper;
    mc_wrapper =(MCWrapper) JSON.deserialize(res.getBody(),MCWrapper.class); 
    System.debug('Resultaat deserializing: '+ mc_wrapper.mortgageContract);
    return mc_wrapper.mortgageContract;
}
  • It works, it sounds logical.. Thank you very much! – Jeroen Joris Jun 20 '18 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.