如何在将 POCO 类序列化为 JSON 字符串时编写空属性



我有一个类如下

public class User
    {
        [JsonProperty("username")]
        string userName;
        public string Username {
            get { return userName + " Jack"; }
            set { userName = value; }
        } 
        [JsonProperty("name")]
        public string Name { get; set; }
        [JsonProperty("location")]
        public string Location { get; set; }

        [JsonProperty("badges")]
        public List<Badge> Badges { get; set; }
        [JsonProperty("accounts")]
        public List<Account> Accounts { get; set; }
    }

    public class Account
    {
        [JsonProperty("role")]
        public string Role = "Default";
        [JsonProperty("description")]
        public string Description = null;
        [JsonProperty("accessLevel")]
        public string AccessLevel = null;
    }
    public class Badge
    {
        [JsonProperty("name")]
        public string Name = null;
        [JsonProperty("description")]
        public string Description;
        [JsonProperty("created")]
        public string Created;
        [JsonProperty("badge")]
        public string BadgeUrl = null;
    }

我正在将 JSON 反序列化为此类,如下所示,请注意,在 JSON 中未传递帐户部分

{
  "username": "mdeiters",
  "Username": "mdeiters Sathish",
  "location": "San Francisco",
  "endorsements": 120,
  "team": "14748",
  "badges": [
    {
      "name": "Charity",
      "description": "Fork and commit to someone's open source project in need",
      "created": "2013-06-14T22:40:45Z",
      "badge": "https://d3levm2kxut31z.cloudfront.net/assets/badges/charity-b7c8758441a70e8378d6332ec15ef292.png"
    },
    {
      "name": "KO Judge",
      "description": "Official Judge of the 2012 Node Knockout",
      "created": "2012-11-30T01:39:08Z",
      "badge": "https://d3levm2kxut31z.cloudfront.net/assets/badges/ko-judge-2012-4e9a536d23bf17b7401de8a6f6e28024.png"
    }
  ]
 }

之后,我将这个POCO类序列化为JSON字符串

var user = JsonConvert.DeserializeObject<User>(strInput);
string output = JsonConvert.SerializeObject(user, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Include });

但是当我检查我的输出字符串用户时,它会给出

"{rn  "username": "mdeiters Sathish",rn  "Username": "mdeiters Sathish Sathish",rn  "name": null,rn  "location": "San Francisco",rn  "endorsements": 120,rn  "team": "14748",rn  "badges": [rn    {rn      "name": "Charity",rn      "description": "Fork and commit to someone's open source project in need",rn      "created": "2013-06-14T22:40:45Z",rn      "badge": "https://d3levm2kxut31z.cloudfront.net/assets/badges/charity-b7c8758441a70e8378d6332ec15ef292.png"rn    },rn    {rn      "name": "KO Judge",rn      "description": "Official Judge of the 2012 Node Knockout",rn      "created": "2012-11-30T01:39:08Z",rn      "badge": "https://d3levm2kxut31z.cloudfront.net/assets/badges/ko-judge-2012-4e9a536d23bf17b7401de8a6f6e28024.png"rn    }rn  ],rn  "accounts": nullrn}"

其中帐户为空,我希望帐户写

如下
"accounts": [rn    {rn      "role": null,rn      "description": null,rn      "accessLevel": nullrn    }rn  ]rn}"

请帮助我解决这个问题

一方面,您的 JSON 字符串没有accounts属性,这意味着您的 User 对象将具有Accounts属性作为NULL,另一方面,在您的输出中,您希望Account属性的对象NULL而不是属性本身。因此,可以通过两种方式实现。1) 您需要使用以下 json 字符串作为输入。

{
    "username": "mdeiters",
    "Username": "mdeiters Sathish",
    "location": "San Francisco",
    "endorsements": 120,
    "team": "14748",
    "badges": [
        {
            "name": "Charity",
            "description": "Fork and commit to someone's open source project in need",
            "created": "2013-06-14T22:40:45Z",
            "badge": "https://d3levm2kxut31z.cloudfront.net/assets/badges/charity-b7c8758441a70e8378d6332ec15ef292.png"
        },
        {
            "name": "KO Judge",
            "description": "Official Judge of the 2012 Node Knockout",
            "created": "2012-11-30T01:39:08Z",
            "badge": "https://d3levm2kxut31z.cloudfront.net/assets/badges/ko-judge-2012-4e9a536d23bf17b7401de8a6f6e28024.png"
        }
    ],
    "accounts": [
        {}
    ]
}

或2) 反序列化对象后,您必须初始化 accounts 属性

var user = JsonConvert.DeserializeObject<User>(strInput);
user.Accounts = new List<Account>();
user.Accounts.add(new Account());
string output = JsonConvert.SerializeObject(user, Formatting.Indented, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Include });

任何这些都应该产生预期的结果。

您要序列化 NULL 值? 如果我正确发现了您的问题,那么您可以查看 JsonSerializerSettings 类,在此类中查看 NullValueHandling 属性并检查它是否可以为您提供帮助。

相关内容

  • 没有找到相关文章

最新更新