You need to sign in to do that
Don't have an account?
Quick question on JSON deserialize
Hi all,
This is my first attempt at using JSON in APEX, seems pretty straight forward. I have what I think is a very simple question...making a call out to a webservice that is returning a JSON string for new order information. In my apex class, I have a simple order class:
public class OrderInfo{
public String CustomerName {get; set;}
public String CustomerNo {get; set;}
public String OrderStatus {get; set;}
public String CustomerPONo{get; set;}
public String Name {get; set;}
public String GrossAmount {get; set;}
public String WantedDeliveryDate {get; set;}
}
I make a call out to my webservice and get back my JSON string...all is well. Now I need to deserialize it which I'm doing like this:
HTTPResponse res = h.send(req); //response comes back correctly, data is all formatted as I would expect..
//deserialize the JSON data returned by the web service into the OrderInfo class defined above
OrderInfo newOrder = new OrderInfo();
newOrder = (OrderInfo)JSON.deserialize(res.getBody(), OrderInfo.class);
So here is my problem, when I look at the values of my newOrder object they are all null. Based on examples I viewed, It looked like the deserialize method with a specified object type would update the values in the object. Do I need to do something else to save the values into the newOrder object?
I really appreciate any assistance, I'm sure it's something very simple! Thanks in advance!!
<pre>
OrderInfo newOrder = (OrderInfo)JSON.deserialize(res.getBody(), OrderInfo.class);
</pre>
Most likely, there's a problem with the JSON string itself. By default, the JSON parser ignores blank or missing values, resulting in the values being null. You could try using JSON.deserializeStrict instead, which throws an error if any properties are left unfilled.
I would check the values of res.getBody() and see what it is actually delivering to you, and make sure that the names and values are present and of the correct capitalization (CustomerName is not the same as customerName).
All Answers
<pre>
OrderInfo newOrder = (OrderInfo)JSON.deserialize(res.getBody(), OrderInfo.class);
</pre>
Most likely, there's a problem with the JSON string itself. By default, the JSON parser ignores blank or missing values, resulting in the values being null. You could try using JSON.deserializeStrict instead, which throws an error if any properties are left unfilled.
I would check the values of res.getBody() and see what it is actually delivering to you, and make sure that the names and values are present and of the correct capitalization (CustomerName is not the same as customerName).