+ Start a Discussion
debikendebiken 

Deserialize JSON list of objects

Hey all,
This is probably really simple, but I've spent all morning and don't have it working.  I have a .NET webservice that my APEX code calls which returns back a list of orders.  It sends an http request to get the service to get the data.  The request being made is getOrders, but the result that comes back is "getOrdersResult", like this:

{"GetOrdersResult":[{"ActualBudget":"0.00","ActualCompletionDate":null,"ActualStartDate":null,"CustomerNo":"SS124","CustomerOrderNo":"","Directive":"New Test WO","EntryDate":"03\/12\/2014","PlannedBudget":"0.00","PlannedCompletionDate":"03\/13\/2014","PlannedStartDate":"03\/12\/2014","WorkOrderNo":"185815","WorkOrderStatus":"WORKREQUEST"},{"ActualBudget":"0.00","ActualCompletionDate":null,"ActualStartDate":null,"CustomerNo":"19231","CustomerOrderNo":"54749","Directive":"Two tower GARP inspection in San Antonio, TX","EntryDate":"01\/16\/2014","PlannedBudget":"0.00","PlannedCompletionDate":"","PlannedStartDate":"","WorkOrderNo":"179316","WorkOrderStatus":"RELEASED"}]}

In my apex I have a public class "myOrder" that contains all the values above.  My problem is deserializing this data into a list when it has a "parent", GetOrderResult. Currently I have this:
List<myOrder> myOrderList = ((GetOrders)System.JSON.deserializeStrict(res.getBody(), GetOrders.class)).GetOrdersResult;

Obviously this is an illegal assignment because "GetOrdersResult" is not viewed as a list.  How do I deserialize into a list of myOrder objects?
Any help is much appreciated!

Abhi__SFDCAbhi__SFDC
There are 2 methods 2 deserialize that 

1. Use this tool to Convert your JSOn to apex class
http://www.adminbooster.com/tool/json2apex

>> Create above apex Class (e.g. jsonResponse.cls)
>> In your code where you are getting the response, use
jsonResponse js {get;set}; // if you want to access the elemnts on VFpage

js=(jsonResponse)JSON.deserialize(response, jsonResponse.class);

you can access the values through js variable

Here's your Apex Class -:
//
//Generated by AdminBooster
//

public class fromJSON{
public cls_GetOrdersResult[] GetOrdersResult;
class cls_GetOrdersResult {
  public String ActualBudget; //0.00
  public cls_ActualCompletionDate ActualCompletionDate;
  public cls_ActualStartDate ActualStartDate;
  public String CustomerNo; //SS124
  public String CustomerOrderNo; //
  public String Directive; //New Test WO
  public String EntryDate; //03/12/2014
  public String PlannedBudget; //0.00
  public String PlannedCompletionDate; //03/13/2014
  public String PlannedStartDate; //03/12/2014
  public String WorkOrderNo; //185815
  public String WorkOrderStatus; //WORKREQUEST
}
class cls_ActualCompletionDate {
}
class cls_ActualStartDate {
}
public static fromJSON parse(String json){
  return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
}

static testMethod void testParse() {
  String json=  '{"GetOrdersResult":[{"ActualBudget":"0.00","ActualCompletionDate":null,"ActualStartDate":null,"CustomerNo":"SS124","CustomerOrderNo":"","Directive":"New Test WO","EntryDate":"03\/12\/2014","PlannedBudget":"0.00","PlannedCompletionDate":"03\/13\/2014","PlannedStartDate":"03\/12\/2014","WorkOrderNo":"185815","WorkOrderStatus":"WORKREQUEST"},{"ActualBudget":"0.00","ActualCompletionDate":null,"ActualStartDate":null,"CustomerNo":"19231","CustomerOrderNo":"54749","Directive":"Two tower GARP inspection in San Antonio, TX","EntryDate":"01\/16\/2014","PlannedBudget":"0.00","PlannedCompletionDate":"","PlannedStartDate":"","WorkOrderNo":"179316","WorkOrderStatus":"RELEASED"}]}';
  fromJSON obj = parse(json);
  System.assert(obj != null);
}
}



2. Use SFDC's JSON parser methods -: 

JSONParser parser =   JSON.createParser(JSONContent);
parser.nextToken();
// Advance to the next value.
 
parser.nextValue();
// Get the field name for the current value.
   
String fieldName = parser.getCurrentName();
// Get the textual

http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_methods_system_jsonparser.htm