返回

c#-Newtonsoft反序列化应为空而不是null

发布时间:2022-07-11 17:55:07 292
# flask

当我将 JSON 字符串反序列化为一个类时,我需要一些属性来获取空格式,而不是 null。

今天:

{
    "OrderResponse": {
        "ID": {
            "Content": "xxx"
        },
        "IssueDate": {
            "Content": "2022-07-07"
        },
        "IssueTime": {
            "Content": "14:42:07"
        },
        "OrderReference": {
            "ID": {
                "Content": "xxx"
            }
        },
        "SellerSupplierParty": {
            "PartyIdentification": null
        },
        "BuyerCustomerParty": {
            "PartyIdentification": null
        },...

正如你们所见PartyIdentification是无效的.

我需要这样:

{
  "OrderResponse": {
    "ID": {
      "Content": "xxxx"
    },
    "IssueDate": {
      "Content": "2022-07-06"
    },
    "IssueTime": {
      "Content": "14:27:35"
    },
    "OrderReference": {
      "ID": {
        "Content": "OKNqIBEo"
      }
    },
    "BuyerCustomerParty": {
      "Party": {
        "PartyIdentification": {
          "ID": {}
        }
      }
    },
    "SellerSupplierParty": {
      "Party": {
        "PartyIdentification": {
          "ID": {}
        }
      }
    },...

类定义如下所示:

public class SftiOrderResponse
    {
        public OrderResponse OrderResponse { get; set; }
    }

    public class OrderResponse
    {
        [JsonProperty(PropertyName = "ID")]
        public OrderId Id { get; set; }
        public IssueDate IssueDate { get; set; }
        public IssueTime IssueTime { get; set; }
        public OrderReference OrderReference { get; set; }
        public Party SellerSupplierParty { get; set; }
        public Party BuyerCustomerParty { get; set; }
        public IList OrderLine { get; set; }
    }
        public class Party
        {
            [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
            public PartyIdentification PartyIdentification { get; set; }
        }

        public class PartyIdentification
        {
            [JsonProperty(PropertyName = "ID")]
            public PartyIdentificationId Id { get; set; }
        }

        public class PartyIdentificationId : StringContent { }
public class StringContent
{
    public string Content { get; set; }
}

最好的方法是什么?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像