使用RestSharp使用子元素反序列化OData类型JSON

时间:2018-10-18 15:23:18

标签: c# json odata restsharp

所以我有这个Json OData响应:

{
    "@odata.context": "http://localhost:7548/DynamicsNAV110/api/beta/$metadata#companies(9b562fdb-5071-4aef-8bf6-d7aa6fd0b472)/salesOrders",
    "value": [
        {
            "@odata.etag": "W/\"JzQ0O2R4VUFBQUo3LzFBQVZnQXhBRGdBTFFBd0FEQUFNQUF3QURrQUFBQUFBQT09Nzs4MTAwMDQyMDsn\"",
            "id": "ae5dddef-397b-4fda-bfc2-7fb6490b295d",
            "number": "PV18-00009",
            "customerNumber": "10780",
            "billingPostalAddress": {
                "street": "HONDURAS N'.43",
                "city": "AGUIMES",
                "state": "LAS PALMAS",
                "countryLetterCode": "ES",
                "postalCode": "35260"
            },
            "salesOrderLines@odata.context": "http://localhost:7548/DynamicsNAV110/api/beta/$metadata#companies(9b562fdb-5071-4aef-8bf6-d7aa6fd0b472)/salesOrders(ae5dddef-397b-4fda-bfc2-7fb6490b295d)/salesOrderLines",
            "salesOrderLines": [
                {
                    "@odata.etag": "W/\"JzQwO1pCVUFBQUNSNzkxZHJuczUyaysvd24rMlNRc3BYUUNIRUNjQUFBQUExOzAwOyc=\"",
                    "documentId": "ae5dddef-397b-4fda-bfc2-7fb6490b295d",
                    "sequence": 10000,
                    "itemId": "fb326b66-26be-4e38-a53f-301da19f445b",
                    "quantity": 1,
                    "lineDetails": {
                        "number": "0000000",
                        "displayName": "REMATE BLANCO EXTERNO 90"
                    },
                    "unitOfMeasure": {
                        "code": "UDS",
                        "displayName": "Unidades",
                        "symbol": null,
                        "unitConversion": null
                    }
                }
            ]
        }
    ]
}

我正在尝试使用RestSharp反序列化,所以我有这两个类:

public class SalesOrder
        {
            public string number { get; set; }
            public SalesOrderLine[] salesOrderLines { get; set; }
        }

        public class SalesOrderLine
        {
            public string itemId { get; set; }
        }

还有这个自定义包装器:

public class ODataCollectionWrapper<T> where T : class
        {
            public IEnumerable<T> Value { get; set; }
        }

这是我的要求:

var client = new RestClient("http://localhost:7548/DynamicsNAV110/api/beta/companies%289b562fdb-5071-4aef-8bf6-d7aa6fd0b472%29/salesOrders?$filter=number%20eq%20%27PV18-00009%27&$expand=salesOrderLines");
            client.Authenticator = new NtlmAuthenticator();
            var request = new RestRequest(Method.GET);
            IRestResponse response = client.Execute<ODataCollectionWrapper<SalesOrder>>(request);

因此,在调试器中,我可以看到我在response.Data.Value.number中正确具有父元素,但是未加载子SalesOrderLines元素。

关于如何正确序列化的任何想法?

0 个答案:

没有答案