1

I am trying to deserialize this 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
      }
   ]
}

here I give invoiceNumber with space.

After deserialize this data I got invoiceNumber=null plz help me this is my code

public class ParsingJsonExample2 {
    public string input                    {set;get;}    
    public string output                {set;get;}
    public void getdata(){
        input='{"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}]}';
        fromJSON json2Apex =fromJSON.parse(input);
        system.debug(json2apex);
    }
}

inner class:

public class fromJSON{
    public cls_invoiceList[] invoiceList;
    public class cls_invoiceList {
        public decimal totalPrice;    
        public Datetime statementDate;    
        public cls_lineItems[] lineItems;
        public Integer invoiceNumber ;    
    }
    class cls_lineItems {
        public decimal UnitPrice;    
        public Decimal Quantity;    
        public String ProductName;    
    }
    public static fromJSON parse(String json){
        return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
    }

}
2

JSON deserialization is case sensitive and needs to match the properties of the Apex class exactly.

Spaces in Apex are ignored, so the line public Integer invoiceNumber ; is actually compiled as if it was public Integer invoiceNumber;.

So the JSON deserializer is looking for the invoiceNumber field (without the space) and cannot find it, thus assigns it a null value.

If you want to keep this JSON structure, you need to replace "invoiceNumber " with "invoiceNumber" in the JSON string, before deserializing. Try something like this:

public static fromJSON parse(String json){
    return (fromJSON) System.JSON.deserialize(json.replace('invoiceNumber ', 'invoiceNumber'), fromJSON.class);
}
| 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.