我有一个我试图鉴定到对象的字符串,但是每次转换它时,角色属性都不会转换回。
{" loginAttEmptId":235," userProfileId":4," username":" devclient1"," id":null," sisscriptionType":null," null,"角色," client"}}
我尝试了这两种避免方法:
PauperToPresidentSerializedModel serializeModel =
JsonConvert.DeserializeObject<PauperToPresidentSerializedModel>(decUserData);
//PauperToPresidentSerializedModel serializeModel
// = serializer.Deserialize<PauperToPresidentSerializedModel>(decUserData);
均给我和对象,其中角色属性为null,而不是其中"客户端"。
这是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()
{
}
}
您的Roles
字段具有私有 setter。您可以:
-
公开
-
有合同解析器访问私人字段:
contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
-
创建一个自定义合同解析器(我猜您不想走那条路线)。
您需要将所有属性公开以序列化和挑选:
public string[] Roles { get; set; }