【问题标题】:Deserialise OData nested response反序列化 OData 嵌套响应
【发布时间】:2020-07-17 02:23:54
【问题描述】:

我有如下所示的 json(odata 响应),我正在尝试在 C# 中对其进行反序列化。响应如下所示:

 {
"d": {
"results": [
    {
        "__metadata": {
            "id": "http://localhost:7305",
            "uri": "http://localhost:7305",
            "type": "Subject.Classifications"
        },
        "dateTime": "2020-06-25T11:31:51",
        "source": "JHGTY",
        "body": "The offering has now increased ",
        "subject": "Offering- updated",
        "fees": {
                "results": [
                    {
                        "week": "1",
                        "amount":"90.00" 
                    },
                    {
                      "week":"2",
                      "amount":"90.00" 
                    }
                    ]
              }

        "Error": {
            "__deferred": {
                "uri": "http://localhost:7305/"
            }
        }
    },
    {
        "__metadata": {
            "id": "http://localhost:7305",
            "uri": "http://localhost:7305",
            "type": "Subject.Classifications"
        },
        "dateTime": "2020-06-26T11:25:51",
        "source": "XFGFT",
        "body": "The offering has now degraded ",
        "subject": "Offering- updated",
        "Error": {
            "__deferred": {
                "uri": "http://localhost:7305/"
            }
        }
    }
]
}
}

我正在使用以下代码使用 newtonsoft 对其进行反序列化:

dynamic notifications = JsonConvert.DeserializeObject<Root>(response.Content);

我已经创建了像下面这样的模型,并且能够对其进行反序列化并获得结果。

    public class Root
    {
        [JsonProperty("d")]
        public D D { get; set; }
    }

    public class D
    {
        [JsonProperty("results")]
        public List<Student> Results { get; set; }
    }
    public class Student
    {
        public string body{ get; set; }
        public string subject{ get; set; }
        public List<Fee> fees { get; set; }
    }
    public class Fee
    {
        public string week{ get; set; }
        public string amount{ get; set; }
        
    }

当我尝试获取费用数组时出现以下错误,但如果我从学生班级中删除费用属性,它工作正常。

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 
'System.Collections.Generic.List`1[genie.Api.Controllers.ClassificationController+Classifications]' 
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

【问题讨论】:

    标签: c# json json.net deserialization


    【解决方案1】:

    在 JSON 中,费用是一个对象而不是数组。你缺课了,

    将您的 Fees 类更新为此以使其正常工作,

    public class Root
    {
        [JsonProperty("d")]
        public D D { get; set; }
    }
    
    public class D
    {
        [JsonProperty("results")]
        public List<Student> Results { get; set; }
    }
    public class Student
    {
        public string body { get; set; }
        public string subject { get; set; }
        public Fees fees { get; set; }
    }
    public class Fees
    {
        public List<Fee> results { get; set; }
    }
    
    public class Fee
    {
        public string week { get; set; }
        public string amount { get; set; }
    }
    

    有了上面的类,你应该可以正确反序列化了,

    var rootObject = JsonConvert.DeserializeObject<Root>(jsonString);
    

    还注意到您在 Error 属性之前缺少一个逗号...可能是复制粘贴。

    "fees": {
      "results": [
        {
          "week": "1",
          "amount": "90.00"
        },
        {
          "week": "2",
          "amount": "90.00"
        }
      ]
    }, // Here, you need to place a comma.
    "Error": {
      "__deferred": {
        "uri": "http://localhost:7305/"
      }
    }
    

    【讨论】:

    • @jawad...我试过了,它没有崩溃,但结果是空的。我是否必须在属性上添加一些属性,例如 [JsonProperty("results")]?
    • @Jawad..请忽略我之前的评论,因为您的解决方案工作正常。我很糟糕,因为 odata 扩展属性设置不正确。
    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多