function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BHASKAR GANGULYBHASKAR GANGULY 

jason.deserialize is returning null

Hi All,
i am trying to parsing the below jason into my object.but while using the deserialize method its comming as Null.
not sure what i am missinh here.

Jason response :
{"invoiceList":[{"totalPrice":5.5,"statementDate":"2011-10-04T16:58:54.858Z","lineItems":[{"UnitPrice":1.0,"Quantity":5.0,"ProductName":"Pencil"},{"UnitPrice":0.5,"Quantity":1.0,"ProductName":"Eraser"}],"invoiceNumber":1},{"totalPrice":11.5,"statementDate":"2011-10-04T16:58:54.858Z","lineItems":[{"UnitPrice":6.0,"Quantity":1.0,"ProductName":"Notebook"},{"UnitPrice":2.5,"Quantity":1.0,"ProductName":"Ruler"},{"UnitPrice":1.5,"Quantity":2.0,"ProductName":"Pen"}],"invoiceNumber":2}]}

Wrapper class for data structure :
public class WrapperListinvoice {
   public List<invoicelist> invoicepp;
    
    public class invoicelist{
        public double totalprice;
        public DateTime Statementdate;
        public list<Lineitems> sline;
        public integer invoicenum;
    }
    public class Lineitems{
        public double unitprice;
        public double quantity;
        public string productname;
    }
}

calling class :
HttpRequest res= new HttpRequest();
res.setEndpoint('https://docsample.herokuapp.com/jsonSample');
res.setMethod('GET');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Accept','application/json');
  Http hes= new Http();
  httpresponse repp=hes.send(res);
    string s=repp.getBody();
    integer status =repp.getStatusCode();
   
//list<responseBean> assp= new list<responseBean>();
WrapperListinvoice responseBean=(WrapperListinvoice) JSON.deserialize(s,WrapperListinvoice.class);
system.debug(responseBean);


Can somebody please help me to understand what i am missing here.

Thanks,
Bhaskar
Raj VakatiRaj Vakati
The JSON is causing an issue JSON.deserializeUntyped .. To apex to JSON the following json must to be updated 
"UnitPrice":1.0 --> should be change like "UnitPrice":"1.0"


Use the  deserializeUntyped as shown below 
 
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(s);
 
HttpRequest res= new HttpRequest();
res.setEndpoint('https://docsample.herokuapp.com/jsonSample');
res.setMethod('GET');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Accept','application/json');
Http hes= new Http();
httpresponse repp=hes.send(res);
string s=repp.getBody();
integer status =repp.getStatusCode();
System.debug('s'+s);
System.debug('status'+status);


if(status==200){
    Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(s);
Do the for loop and set the values into the wrapper class 
    //list<responseBean> assp= new list<responseBean>();
    //WrapperListinvoice responseBean=(WrapperListinvoice) JSON.deserialize(s,WrapperListinvoice.class);
    system.debug(m);
}


Use the below wrapper class
 
public class WrapperListinvoice {
    public cls_invoiceList[] invoiceList{get;set;}
    class cls_invoiceList {
        public Double totalPrice{get;
                                 set {
                                     totalPrice = 0 ;
                                 }
                                }//5.5
        public String statementDate{get;set {
            statementDate = '';
        }}	//2011-10-04T16:58:54.858Z
        public cls_lineItems[] lineItems{get;set;}
        public Integer invoiceNumber{get; set {
            invoiceNumber = 0 ;
        }}	//1
    }
    class cls_lineItems {
        public Integer UnitPrice{get; set {
            UnitPrice = 0 ;
        }}	//1
        public Integer Quantity{get; set {
            Quantity = 0 ;
        }}	//5
        public String ProductName{get; set {
            ProductName = '' ;
        }}	//Pencil
    }
}


 
BHASKAR GANGULYBHASKAR GANGULY
thanks Raj
Naveen KNNaveen KN
Hi Bhaskar, If the above solution is working as expected, please mark it as an answer. Thanks.