簡體   English   中英

嘗試映射對象時,反序列化對象返回帶有嵌套 JSON 的 null

[英]Deserialize Object returns null with nested JSON when trying to map it

我知道這個問題看起來很熟悉,但就我而言,它有點不同。

  1. 我使用了一個以 XML 格式返回數據的 API。
  2. 獲取 API 響應並將其轉換為 JSON 格式,以便我能夠進行映射。

我的問題,當我 DeserializeObject 它總是返回 null 並且我不太確定這是否是由映射引起的。

問題:如何反序列化嵌套的 JSON 對象以映射字段? 如果我必須在將 XML 轉換為 JSON 方面進行更改,也請指導我

謝謝!

將 XML 響應轉換為 JSON

            var result = await client.GetAsync($"url");

            var xml = result.Content.ReadAsStringAsync().Result;
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            string json = JsonConvert.SerializeXmlNode(doc);

            //the results hear is null for all the properties
            var des = (AccountLite)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(AccountLite));
            Console.WriteLine(des);
            Console.ReadLine();

JSON 對象

{
"MessageEnvelope": {
    "ListOfAccountLite": {
        "AccountLite": [
            {
                "Id": "",
                "AccountStatus": "",
                "AccountTypeCode": "",
                "CSN": "",
                "Location": "",
                "MasterAccountId": "",
                "Name": ""
            },
            {
                "Id": "",
                "AccountStatus": "",
                "AccountTypeCode": "",
                "CSN": "",
                "Location": "",
                "MasterAccountId": "",
                "Name": ""
            },
            {
                "Id": "",
                "AccountStatus": "",
                "AccountTypeCode": "",
                "CSN": "",
                "Location": "",
                "MasterAccountId": "",
                "Name": ""
            }
        ]
    }   
}
}

C#基於JSON對象生成的類

public partial class Welcome
{
    [JsonProperty("MessageEnvelope")]
    public MessageEnvelope MessageEnvelope { get; set; }
}

public partial class MessageEnvelope
{
    [JsonProperty("ListOfAccountLite")]
    public ListOfAccountLite ListOfAccountLite { get; set; }
}

public partial class ListOfAccountLite
{
    [JsonProperty("AccountLite")]
    public AccountLite[] AccountLite { get; set; }
}

public partial class AccountLite
{
    [JsonProperty("Id")]
    public string Id { get; set; }

    [JsonProperty("AccountStatus")]
    public string AccountStatus { get; set; }

    [JsonProperty("AccountTypeCode")]
    public string AccountTypeCode { get; set; }

    [JsonProperty("CSN")]
    public string Csn { get; set; }

    [JsonProperty("Location")]
    public string Location { get; set; }

    [JsonProperty("MasterAccountId")]
    public string MasterAccountId { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }
}

只需反序列化為根類Welcome而不是AccountLite

//var des = (AccountLite)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(AccountLite));

Welcome welcome = JsonConvert.DeserializeObject<Welcome>(json);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM