【问题标题】:Issue with deserializing data using RestSharp使用 RestSharp 反序列化数据的问题
【发布时间】:2018-02-05 18:25:03
【问题描述】:

我在反序列化通过 RestSharp 从 Web 服务返回的数据时遇到了一些困难。以下是 POST 请求返回的数据。

{
"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": ""
}

这是我试图将数据反序列化成的模型。

型号:

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; }
}

这是成功获取数据但似乎无法加载到 SalesOrderModel 列表中的类。

类:

 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;

    }

谁能告诉我如何正确地将返回的数据映射到模型类中?

谢谢

编辑:我更新了我的模型以包含各种“包装器”。

现在我的问题是,在视图中访问这些数据的最佳方式是什么?

编辑: 查看:

@model DirectOrderTracker.Models.RootObject


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

【问题讨论】:

    标签: c# asp.net json web-services restsharp


    【解决方案1】:

    你的 json 和反序列化的模型不兼容。您正在尝试将非集合 json 反序列化为像 List 这样的集合。所以,像这样修改你的模型;

    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; }
    }
    

    JsonObject一样反序列化它;

    IRestResponse<JsonObject> response = client.Execute<JsonObject>(request);
    

    【讨论】:

    • 是的,我找到了 json2c# 转换器。很棒的工具。
    • 我现在的问题是,在视图中访问这些数据的最佳方式是什么?
    • 通过模型将返回的数据传递给视图。
    • 想了很多。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多