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
divya manohardivya manohar 

deserialize issue

Hello 

I am deserialize json input but face error as follows.

I am facing an error as:Unknown field: My_Invoice_list.InvoiceWrapper.invoiceList
Error is in expression '{!deserialize}' in component <apex:commandButton> in page vf_invoicelist: (System Code)
Class.My_Invoice_list.deserialize: line 39, column 1
An unexpected error has occurred. Your solution provider has been notified. (System)

Below is the apex class code and vf page code.
 
public class My_Invoice_list
{
   public class InvoiceList 
   {
        public Double totalPrice {get;set;} 
        public String statementDate {get;set;} 
        public List<LineItems> lineItems {get;set;} 
        public Integer invoiceNumber {get;set;} 
   }
   
   public class LineItems
   {
      public Double UnitPrice {get;set;} 
      public Double Quantity {get;set;} 
      public String ProductName {get;set;} 
   }
  
   public class InvoiceWrapper
   {
      public list<InvoiceList> invoice_wrapper{get;set;}
   }
   
   public InvoiceWrapper invoice{get;set;}
   
   public void deserialize()
   {
   
     Http p = new Http();
     
     HttpRequest request = new HttpRequest();
     
     request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
     
     request.setHeader('Content-type', 'application/json');      
     
     request.setMethod('GET');
     
     HttpResponse response=p.send(request);
     invoice=(InvoiceWrapper)JSON.deserializeStrict(response.getBody(),InvoiceWrapper.class);
          
   }
}
 
<apex:page controller="My_Invoice_list">
<apex:form >

<apex:pageBlock title="Deserialize JSON Resposne">

<apex:pageBlockSection>
<apex:pageBlockSectionItem>

<apex:commandButton value="submit"
                    action="{!deserialize}"
                    reRender="invoicetable"/>
                    
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>            

<apex:pageBlock>
<apex:pageBlockTable value="{!invoice.invoice_wrapper}"
                     var="inv"
                     id="invoicetable">

                      <apex:column value="{!inv.totalPrice}" headerValue="TotalPrice" />
                      <apex:column value="{!inv.statementDate}" headerValue="statementDate" />
                     
</apex:pageBlockTable>
</apex:pageBlock>   
</apex:form>
</apex:page>

I am not understanding how to display the line items. 

divya
 
Best Answer chosen by divya manohar
pconpcon
The following should do what you are looking for

JSONDeserialize.cls
public class JSONDeserialize {
    public class LineItem {
        public Double unitPrice {get; set;}
        public Double quantity {get; set;}
        public String productName {get; set;}
    }
    
    public class Invoice {
        public Double totalPrice {get; set;}
        public DateTime statementDate {get; set;}
        public String contactnumber {get; set;}
        public List<LineItem> lineItems {get; set;}
        public Integer invoiceNumber {get; set;}
    }
    
    public class InvoiceWrapper {
        public List<Invoice> invoiceList {get; set;}
    }
    
    public InvoiceWrapper wrapper {
        get;
        set;
    }
   
    public void deserialize() {
        Http h = new Http();  
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
        request.setHeader('Content-type', 'application/json');     
        request.setMethod('GET');
        
        HttpResponse response = h.send(request);

		wrapper = (InvoiceWrapper) JSON.deserializeStrict(response.getBody(), InvoiceWrapper.class);
	}
}

JSONDeserialize.vfp
<apex:page controller="JSONDeserialize">
    <apex:form >
        <apex:pageBlock title="Deserialize JSON Resposne">
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:commandButton value="submit" action="{!deserialize}" reRender="invoiceTable"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>            
        <apex:pageBlock>
            <apex:pageBlockTable value="{!wrapper.invoiceList}" var="inv" id="invoiceTable">
                <apex:column value="{!inv.totalPrice}" headerValue="TotalPrice" />
                <apex:column value="{!inv.statementDate}" headerValue="statementDate" />
            </apex:pageBlockTable>
        </apex:pageBlock>   
    </apex:form>
</apex:page>

All Answers

pconpcon
The following should do what you are looking for

JSONDeserialize.cls
public class JSONDeserialize {
    public class LineItem {
        public Double unitPrice {get; set;}
        public Double quantity {get; set;}
        public String productName {get; set;}
    }
    
    public class Invoice {
        public Double totalPrice {get; set;}
        public DateTime statementDate {get; set;}
        public String contactnumber {get; set;}
        public List<LineItem> lineItems {get; set;}
        public Integer invoiceNumber {get; set;}
    }
    
    public class InvoiceWrapper {
        public List<Invoice> invoiceList {get; set;}
    }
    
    public InvoiceWrapper wrapper {
        get;
        set;
    }
   
    public void deserialize() {
        Http h = new Http();  
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://docsample.herokuapp.com/jsonSample');
        request.setHeader('Content-type', 'application/json');     
        request.setMethod('GET');
        
        HttpResponse response = h.send(request);

		wrapper = (InvoiceWrapper) JSON.deserializeStrict(response.getBody(), InvoiceWrapper.class);
	}
}

JSONDeserialize.vfp
<apex:page controller="JSONDeserialize">
    <apex:form >
        <apex:pageBlock title="Deserialize JSON Resposne">
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:commandButton value="submit" action="{!deserialize}" reRender="invoiceTable"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>            
        <apex:pageBlock>
            <apex:pageBlockTable value="{!wrapper.invoiceList}" var="inv" id="invoiceTable">
                <apex:column value="{!inv.totalPrice}" headerValue="TotalPrice" />
                <apex:column value="{!inv.statementDate}" headerValue="statementDate" />
            </apex:pageBlockTable>
        </apex:pageBlock>   
    </apex:form>
</apex:page>
This was selected as the best answer
divya manohardivya manohar
Hi
still, I am getting the same bug
 
pconpcon
You get the same error if you copy and paste my controller and page in?  Did you make any modifications?
divya manohardivya manohar
Hi
ur code works fine , but I am puzzled becaue its the same code I have executed , only class names change.
can u tell me how to display line items?
pconpcon
Can you please paste the exact code you currently have and incude the file name outside the code samples
divya manohardivya manohar
Hi
I made a change as follows:
The trick was the attribute "breakBefore"

<apex:column breakBefore="true" 
                             colspan="2">

<apex:pageBlockTable value="{!inv.lineitems}"
                                     var="line">
                                                     
                 <apex:column value="{!line.unitPrice}" 
                              headerValue="UnitPrice"/>
                 
                 <apex:column value="{!line.quantity}" 
                              headerValue="Quantity"/>
                              
                 <apex:column value="{!line.productName}" 
                              headerValue="productName"/>
                              
                </apex:pageBlockTable>

I got what I wanted.
Thanks for ur help.