简体   繁体   English

将json反序列化为对象时调用构造函数

[英]Call the constructor when Deserializing json to object

I have the LoginModels :我有LoginModels

public class LoginModels
{
    public LoginModels(string userEmail, string userPassword)
    {
        email = userEmail;
        password = userPassword;
        errorMessage = GetLoginError();
    }

    public string email;
    public string password;
    public string errorMessage;

    public string GetLoginError()
    {
        if (string.IsNullOrEmpty(email)) return "email is empty";
        else return "good";
    }
}

I sent a json to a function of a controller..我向控制器的函数发送了一个 json ..

In the controller, I wrote:在控制器中,我写道:

LoginModels user = JsonConvert.DeserializeObject<LoginModels>(userDetails);
string relevantEmail = user.email;

BUT the constructor of LoginModels gets email and password as null.但是LoginModels的构造函数将emailpassword设为 null。

That's why errorMessage is email is empty .这就是errorMessage is email is empty的原因。

But relevantEmail is the email that came from the ajax (and it's ok).relevantEmail电子邮件是来自 ajax 的电子邮件(没关系)。

I realy don't know why the constructor doesn't get the parameters that were send by the ajax call.我真的不知道为什么构造函数没有得到 ajax 调用发送的参数。

Any help appreciated!任何帮助表示赞赏!

Serialization/deserialization can call only default constructor - imagine you'll have multiple constructors with various parameters - how can the framework guess which one to call/which parameters ?序列化/反序列化只能调用默认构造函数 - 假设您将有多个带有各种参数的构造函数 - 框架如何猜测要调用哪个/哪些参数? Additionally the serializable fields should be properties.此外,可序列化的字段应该是属性。 So your object should look like:所以你的对象应该是这样的:

public class LoginModels
{
    private string _errorMessage;

    // default ctor for serialization
    public LoginModels() 
    {
    }

    public LoginModels(string userEmail, string userPassword)
    {
        email = userEmail;
        password = userPassword;
    }

    public string email { get; set; }
    public string password { get; set; }
    public string errorMessage 
    { 
        get 
        { 
            if (string.IsNullOrEmpty(_errorMessage))
            {
                _errorMessage = GetLoginError();
            }
            return _errorMessage;
        } 
        set { _errorMessage = value; }
    }

    public string GetLoginError()
    {
        if (string.IsNullOrEmpty(email))
        {
            return "email is empty";
        }
        // also no need for "else" here
        return "good";
    }
}

Use the JsonConstructor attribute so that your JsonConvert knows which constructor to use:使用 JsonConstructor 属性,以便您的 JsonConvert 知道要使用哪个构造函数:

using using Newtonsoft.Json;;    

public class LoginModels
{
  [JsonConstructor]
  public LoginModels(string userEmail, string userPassword)
  {
      email = userEmail;
      password = userPassword;
      errorMessage = GetLoginError();
  }

  public string email;
  public string password;
  public string errorMessage;

  public string GetLoginError()
  {
      if (string.IsNullOrEmpty(email)) return "email is empty";
      else return "good";
  }
}

Here is the source: https://www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm这是来源: https : //www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM