0

I just Tested this code, When I'm running this JSON not assigning to the variables it is coming NUll.

String requestString='{"terms":{"customerCode":"sadasd","customerName":"sadasd","incoterm":"EXW","paymentTerms":"T30JFDM","paymentTermsDescriptionEn":"Transfer 30 days end of month the 20th","paymentTermsDescriptionFr":"Virement 30 jours Fin de Mois le 20","creditLimit":"asdasdsa"}}';
              system.debug('string json'+requestString);
System.debug(
    system.JSON.deserialize(requestString,terms.class));

terms quoweb=(terms)JSON.deserialize(requestString,terms.class);  
              //quoteData quoweb=(quoteData)JSON.deserialize(json,quoteData.class);

              integer i=0;
Try{
              System.debug('web serivice size JSO'+quoweb);
             // for(i=0;i<quoweb.terms.size();i++){

               //   System.debug('web serivice JSON checking size and detailes'+quoweb.terms.size()+'Detailes'+quoweb.terms[i].paymentTerms);

              //}
}catch(exception e) {

    System.debug('excdsdf'+e);

}


public class terms{
        public String customerCode{get;set;}
        public String customerName{get;set;}
        public String incoterm{get;set;}
        public String paymentTerms{get;set;}
        public String paymentTermsDescriptionEn{get;set;}
        public String paymentTermsDescriptionFr{get;set;}
        public String creditLimit{get;set;}
}

What I'm Getting:

USER_DEBUG [5]|DEBUG|terms:[creditLimit=null, customerCode=null, customerName=null, incoterm=null, paymentTerms=null, paymentTermsDescriptionEn=null, paymentTermsDescriptionFr=null]
1

Your input JSON should not include the outer object with the "terms" property... change it to:

String requestString='{"customerCode":"sadasd","customerName":"sadasd","incoterm":"EXW","paymentTerms":"T30JFDM","paymentTermsDescriptionEn":"Transfer 30 days end of month the 20th","paymentTermsDescriptionFr":"Virement 30 jours Fin de Mois le 20","creditLimit":"asdasdsa"}';
| improve this answer | |
0

The issue is that your JSON isn't just a {<terms object>} as your code assumes. There's an extra layer of object in there that you haven't captured. It's really a {<property name> : <terms object>}.

Removing the outer layer of object, as Phil W suggests, is one way to going about fixing this. That approach isn't always practical (especially if you're getting the JSON from some external third party).

You could simply do an untyped deserialization, but probably the easiest way to handle this in most real applications would be to add another class so that you have an accurate representation of your JSON's structure.

In this case, the extra class is pretty easy.

public class OuterObject{
    // Using the name of a property as the class name isn't ideal.
    // This will, however, work
    Terms terms;
}

You'd then be using OuterObject in the deserialization in place of Terms, and accessing the data inside of the Terms object would be done in the normal way (e.g. myOuterObject.terms.customerCode)

| improve this answer | |

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.