1

I'm having some difficulty deserializing data returned from a web service via RestSharp. Below is the data returned from a POST request.

{
"responseCPUTime": "0",
"response": "PASS",
"responseFor": "getSalesOrders",
"responseData": {
"salesOrders": [
  {
            "loadStatus": "",
            "comments": "",
            "modifyDate": "20180130",
            "custShipToID": "",
            "invoiceCount": "0",
            "custBillToID": "WINDJ",
            "invoiceAmount": "12280.00",
            "pickDate": "20180204",
            "warehouse": "~D",
            "custName": "WINN DIXIE JACKSONVILLE",
            "modifyTime": "102614",
            "loadID": "",
            "createTime": "075610",
            "createdBy": "RGN",
            "custPONum2": "",
            "modifiedBy": "KAL",
            "SONum": "00855494",
            "deliveryDate": "20180205",
            "tripNumber": "",
            "custPONum": "66523",
            "status": "O",
            "createDate": "20180125"
            },
              {
            "loadStatus": "",
            "comments": "",
            ......
            }
            ],
    },
"responseMessage": ""
}

Here is the model I'm trying to deserialize the data into.

Model:

public class SalesOrder
{
    public string loadStatus { get; set; }
    public string comments { get; set; }
    public string modifyDate { get; set; }
    public string custShipToID { get; set; }
    public string invoiceCount { get; set; }
    public string custBillToID { get; set; }
    public string invoiceAmount { get; set; }
    public string pickDate { get; set; }
    public string warehouse { get; set; }
    public string custName { get; set; }
    public string modifyTime { get; set; }
    public string loadID { get; set; }
    public string createTime { get; set; }
    public string createdBy { get; set; }
    public string custPONum2 { get; set; }
    public string modifiedBy { get; set; }
    public string SONum { get; set; }
    public string deliveryDate { get; set; }
    public string tripNumber { get; set; }
    public string custPONum { get; set; }
    public string status { get; set; }
    public string createDate { get; set; }
}

public class ResponseData
{
    public List<SalesOrder> salesOrders { get; set; }
}

public class RootObject
{
    public string responseCPUTime { get; set; }
    public string response { get; set; }
    public string responseFor { get; set; }
    public ResponseData responseData { get; set; }
    public string responseMessage { get; set; }
}

Here is the class that successfully fetches the data but cannot seem to load into a List of SalesOrderModel.

Class:

 public List<SalesOrderModel> GetSalesOrders(string company, string custBillToID, string startShipDate, string endShipDate)
    {
        var client = new RestClient("https://xxxxxxx.xxxxxx.com");
        var request = new RestRequest("xxx/services", Method.POST);
        request.AddHeader("header", @"accept: application/json
                                        accept-encoding: gzip, deflate
                                        accept-language: en-US, en; q=0.8
                                        content-type: application/json
                                        user-agent: Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

        request.AddParameter("appId", "xxxxxx");
        request.AddParameter("command", "getSalesOrders");
        request.AddParameter("company", "0007");
        request.AddParameter("startDeliveryDate", "20180205");
        request.AddParameter("endDeliveryDate", "20180205");
        request.AddParameter("status", "O");


        IRestResponse <List<SalesOrderModel>> response = client.Execute<List<SalesOrderModel>>(request);
        return response.Data;

    }

Can anyone point me into the direction as to how to properly map the data returned into a model class?

Thanks

Edit: I updated my model to include a 'wrapper' of sorts.

Now my question is, what is the best way to access this data in a view?

Edit: View:

@model DirectOrderTracker.Models.RootObject


@foreach (var item in @Model.responseData.salesOrders)
{
    @item.custBillToID
}

1 Answer 1

2

Your json and the model which are deserialized is incompitable. You are trying to deserialize non-collection json as a collection like List. So, modify your model like this;

public class SalesOrder
{
    public string loadStatus { get; set; }
    public string comments { get; set; }
    public string modifyDate { get; set; }
    public string custShipToID { get; set; }
    public string invoiceCount { get; set; }
    public string custBillToID { get; set; }
    public string invoiceAmount { get; set; }
    public string pickDate { get; set; }
    public string warehouse { get; set; }
    public string custName { get; set; }
    public string modifyTime { get; set; }
    public string loadID { get; set; }
    public string createTime { get; set; }
    public string createdBy { get; set; }
    public string custPONum2 { get; set; }
    public string modifiedBy { get; set; }
    public string SONum { get; set; }
    public string deliveryDate { get; set; }
    public string tripNumber { get; set; }
    public string custPONum { get; set; }
    public string status { get; set; }
    public string createDate { get; set; }
}

public class ResponseData
{
    public List<SalesOrder> salesOrders { get; set; }
}

public class JsonObject
{
    public string responseCPUTime { get; set; }
    public string response { get; set; }
    public string responseFor { get; set; }
    public ResponseData responseData { get; set; }
    public string responseMessage { get; set; }
}

And deserialize it like JsonObject;

IRestResponse<JsonObject> response = client.Execute<JsonObject>(request);
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I found that json2c# converter. Great tool.
My question is now, what is the best way to access this data in a view?
Pass the returned data to the view via model.
Figured as much. Thanks for the help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.