gpt4 book ai didi

c# - 无法用空值序列化 json

转载 作者:行者123 更新时间:2023-11-30 12:24:35 26 4
gpt4 key购买 nike

 [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,但出现错误。

An exception of type 'Newtonsoft.Json.JsonException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Error creating 'LinkListOfUserGroup'.

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

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

应该可以解决问题。

关于c# - 无法用空值序列化 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33741032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com