我有这个字符串,我试图反序列化为一个对象,但每次转换时,角色属性都不会被转换回来。
{ “LoginAttemptId”:235, “UserProfileId”:4 “用户名”: “devclient1”, “标识”:NULL, “SubscriptionType”:空, “角色”:[ “客户端”]}
我已尝试过这两种反序列化方法:
PauperToPresidentSerializedModel serializeModel =
JsonConvert.DeserializeObject<PauperToPresidentSerializedModel>(decUserData);
//PauperToPresidentSerializedModel serializeModel
// = serializer.Deserialize<PauperToPresidentSerializedModel>(decUserData);
并且都给了我和对象,其中Roles属性为Null,而不是其中包含'Client'。
这是PauperToPresidentSerializedModel obejct定义
public class PauperToPresidentSerializedModel
{
public long LoginAttemptId { get; set; }
public long UserProfileId { get; set; }
public string Username { get; set; }
public string Id { get; set; }
public string SubscriptionType { get; set; }
public string[] Roles { get; private set; }
public PauperToPresidentSerializedModel(PauperToPresidentPrincipal principal)
{
LoginAttemptId = principal.LoginAttemptId;
UserProfileId = principal.UserProfileId;
Username = principal.Username;
Id = principal.Id;
SubscriptionType = principal.SubscriptionType;
Roles = principal.Roles;
}
public PauperToPresidentSerializedModel()
{
}
}
答案 0 :(得分:2)
您的Roles
字段有一个私有设置器。你可以:
让合同解析程序访问私有字段:
contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
创建自定义合约解析器(我猜你不想走那条路)。
答案 1 :(得分:0)
您需要公开所有属性才能序列化和反序列化:
public string[] Roles { get; set; }