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

时间:2015-04-11 02:36:58

标签: .net json.net

我有一个类如下

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 });

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

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

帐户为空,我希望帐户写成如下

\"accounts\": [\r\n    {\r\n      \"role\": null,\r\n      \"description\": null,\r\n      \"accessLevel\": null\r\n    }\r\n  ]\r\n}"

请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

你想序列化NULL值吗?如果我正确地找到你的问题,那么你可以看一下JsonSerializerSettings类,在这个类里面看看NullValueHandling属性并检查它是否可以帮到你。

答案 1 :(得分:0)

一方面,您的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 });

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