无法序列化具有空值的 json


 [JsonObject(MemberSerialization.OptIn)]
public class NameIDItem
{
    [JsonProperty]
    public string _name{ get; set; }
    [JsonProperty]
    public int _id { get; set; }
    public NameIDItem(){}
    public NameIDItem(string name, int id)
    {
        _name = name;
        _id = id;
    }
}

[JsonObject(MemberSerialization.OptIn)]
public class LinkListOfUserGroup
{
    public List<NameIDItem> NewItems { get; set; }
    public List<NameIDItem> RemovedItems { get; set; }
    public bool TrackChanges { get; set; }
    public List<NameIDItem> Items { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class UserRequestJson
{
    [JsonProperty(Required = Required.Always)]
    public string Email { get; set; }
    [JsonProperty]
    public string Email2 { get; set; } 
    [JsonProperty]
    public string Email3 { get; set; }
    [JsonProperty(Required = Required.Always)]
    public string UserName { get; set; }
    [JsonProperty(Required = Required.Always)]
    public string Password { get; set; }
    [JsonProperty(Required = Required.Always)]
    public string PasswordQuestion { get; set; }
    [JsonProperty(Required = Required.Always)]
    public string PasswordAnswer { get; set; }
    [JsonProperty]
    public bool IsAdmin { get; set; }
    [JsonProperty]
    public bool MustResetPassword { get; set; }
    [JsonProperty]
    public byte Source {get {return source;} set { source = value; }}
    private byte source = 0;
    [JsonProperty]
    public string CellPhoneNo { get; set; }
    [JsonProperty]
    public string CellPhoneNo2 { get; set; }
    [JsonProperty]
    public string CellPhoneNo3 { get; set; }
    [JsonProperty]
    public string PagerNo { get; set; }
    [JsonProperty]
    public string PagerNo2 { get; set; }
    [JsonProperty]
    public bool NotifyBySMS { get; set; }
    [JsonProperty]
    public bool NotifyByEmail { get; set; }
    [JsonProperty]
    public bool NotifyByPopup { get; set; }
    [JsonProperty]
    public bool NotifyByPush { get; set; }
    [JsonProperty]
    public bool AllowPersonalDashboard { get; set; }
    [JsonProperty]
    public bool AllowUnlimitedDashboardItems { get; set; }
    [JsonProperty]
    public bool CanHaveUnlimitedAlerts { get; set; }
    [JsonProperty]
    public bool StayLoggedIn { get; set; }
    [JsonProperty]
    public bool IsActive { get; set; }
    [JsonProperty]
    public int AccountTypeID {get {return accountTypeID;} set { accountTypeID = value; }}
    private int accountTypeID = 1;
    [JsonConverter(typeof(GERLinkListOfUserGroup))]
    public GERLinkListOfUserGroup Groups { get; set; }
    [JsonProperty]
    public int CreatedBy { get; set; }
    [JsonProperty]
    public string CreatedOn  {get {return createdOn;} set { createdOn = value; }}
    private string createdOn = "0001-01-01T00:00:00";
    [JsonProperty]
    public int ModifiedBy { get; set; }
    [JsonProperty]
    public string ModifiedOn {get {return modifiedOn;} set { modifiedOn = value; }}
    private string modifiedOn = "0001-01-01T00:00:00";
    [JsonProperty]
    public int ID { get; set; }
    [JsonProperty(Required = Required.Always)]
    public string Name { get; set; }
    [JsonProperty(Required = Required.Always)]
    public string ConfirmPassword { get; set; }
    [JsonProperty]
    public int CellProviderID { get; set; }
    [JsonProperty]
    public int CellProviderID2 { get; set; }
    [JsonProperty]
    public int CellProviderID3 { get; set; }
    [JsonProperty]
    public int PagerProviderID { get; set; }
    [JsonProperty]
    public int PagerProviderID2 { get; set; }
}

我尝试序列化 json,但出现错误。

类型的异常 "Newtonsoft.Json.JsonException"发生在Newtonsoft.Json中.dll但未在用户代码中处理

其他信息:创建"链接列表用户组"时出错。

我应该怎么做才能获得空列表的 [],空字符串和整数的 "?

        UserRequestJson user = new UserRequestJson()
        {
            Email = _Name + "@email.com",
            UserName = _Name,
            Password = _Name + "!",
            PasswordQuestion= _Name,
            PasswordAnswer= _Name,
            Groups = new GERLinkListOfUserGroup()
            {
                NewItems = new List<NameIDItem>() { new NameIDItem("HCS Users", groupID) },
                RemovedItems = new List<NameIDItem>(),
                TrackChanges = false,
                Items = new List<NameIDItem>() { new NameIDItem("HCS Users", groupID) }
            },
            Name=_Name,
            ConfirmPassword= _Name + "!"
        };
        string json= JsonConvert.SerializeObject(user);
        Console.WriteLine(json);

{"Email":"asdasd2@mail.com","Email2":"","Email3":"","UserName":"!asfsfsd","Password":"!asdasd!asdasd","PasswordQuestion":"!asdasd","PasswordAnswer":"!asdasd","IsAdmin":false,"Source":0,"MustResetPassword":false,"CellPhoneNo":"","CellPhoneNo2":"","CellPhoneNo3":"","PagerNo":"","PagerNo2":"","NotifyBySMS":false,"NotifyByEmail":false,"NotifyByPopup":false,"NotifyByPush":false,"AllowPersonalDashboard":false,"AllowUnlimitedDashboardItems":false,"CanHaveUnlimitedAlerts":false,"StayLoggedIn":true,"IsActive":false,"AccountTypeID":1,"Groups":{"NewItems":[],"RemovedItems":[],"TrackChanges":false,"Items":[]},"CreatedBy":0,"CreatedOn":"0001-01-01T00:00:00","ModifiedBy":0,"ModifiedOn":"0001-01-01T00:00:00","ID":0,"Name":"!asdasd","ConfirmPassword":"!asdasd!asdasd","CellProviderID":"","CellProviderID2":"","CellProviderID3":"","PagerProviderID":"","PagerProviderID2":""}

您在LinkListOfUserGroup中的任何属性上都没有 [JsonProperty] 属性,这就是它失败的原因。

[JsonObject(MemberSerialization.OptIn)]
public class LinkListOfUserGroup
{
    [JsonProperty]
    public List<NameIDItem> NewItems { get; set; }
    [JsonProperty]
    public List<NameIDItem> RemovedItems { get; set; }
    [JsonProperty]
    public bool TrackChanges { get; set; }
    [JsonProperty]
    public List<NameIDItem> Items { get; set; }
}

应该做这个伎俩。

相关内容

  • 没有找到相关文章

最新更新