类的JSON中的序列化返回null值

时间:2022-05-26 20:27:55

I am working on a universal windows 10 application. I am working with a webAPI and the I am trying to make a post call using JSON serialization of my class, The code is as follows:

我正在研究通用的Windows 10应用程序。我正在使用webAPI,我正在尝试使用我的类的JSON序列化进行调用,代码如下:

public async void PostAPI()
    {
        try
        {
            var postObject = JsonConvert.SerializeObject(thisUser);

            string _serviceUrl = Constants.BaseUrl + "api/Account/Register";
            HttpClient client = new HttpClient();

            await client.PostAsync(new Uri(_serviceUrl), new HttpStringContent(postObject, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json"));
        }
        catch (Exception)
        {
            throw;
        }

    }

the values for thisUser are being taken from textbox's in the UI something like this:

thisUser的值来自UI中的文本框,如下所示:

        RegisterModel thisUser = new RegisterModel();
thisUser.UserDetails = new UserDetails();
        thisUser.Email = UserInputemail.Text;
        thisUser.Password = "addsFABBS!2";
        thisUser.ConfirmPassword = "addsFABBS!2";
        thisUser.UserDetails.FullName = UserInputName.Text;
        thisUser.UserDetails.Username = UserInputUserName.Text;
        thisUser.UserDetails.FullName = UserInputName.Text;
        thisUser.UserDetails.ICEFullName = ICEName.Text;
        thisUser.UserDetails.ICEMobileNumber = int.Parse(ICEPhoneNo.Text);
        thisUser.UserDetails.DoctorFullName = DocName.Text;
        thisUser.UserDetails.DoctorMobileNumber = int.Parse(DocPhoneNo.Text);

The postObject remains null. it has the value {} where as in the thisUser the values are present.

postObject保持为null。它具有值{},其中在thisUser中存在值。

the thisUser is an instance of the RegisterModel class which has the following properties:

thisUser是RegisterModel类的一个实例,它具有以下属性:

  using System.Runtime.Serialization;
  namespace APIValueSetterTest.Model
  {
    [DataContract]
    public class RegisterModel
    {
        public int UserId { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }

        public UserDetails UserDetails { get; set; }
    }

}

and the user details class is as follows:

用户详细信息类如下:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
{
    [DataContract]
    public class UserDetails
    {
        public int UserId { get; set; }

        public string FullName { get; set; }

        public string Username { get; set; }

        public string ICEFullName { get; set; }

        public int ICEMobileNumber { get; set; }

        public string DoctorFullName { get; set; }

        public int DoctorMobileNumber { get; set; }
    }
}

Where am I going wrong?

我哪里错了?

1 个解决方案

#1


2  

I think you need to indicate the properies on this class to be data members, like so:

我认为你需要指出这个类的特性是数据成员,如下所示:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
  {
    [DataContract]
    public class RegisterModel
    {
        [DataMember]      
        public int UserId { get; set; }
        [DataMember]      
        public string Email { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string ConfirmPassword { get; set; }
        [DataMember]
        public UserDetails UserDetails { get; set; }
    }

}

Same goes for the UserDetails class:

UserDetails类也是如此:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
{
    [DataContract]
    public class UserDetails
    {
        [DataMember]
        public int UserId { get; set; }
        [DataMember]
        public string FullName { get; set; }
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string ICEFullName { get; set; }
        [DataMember]
        public int ICEMobileNumber { get; set; }
        [DataMember]
        public string DoctorFullName { get; set; }
        [DataMember]
        public int DoctorMobileNumber { get; set; }
    }
}

Properties not decorated with the DataMember attribute will result in them being ignored by the serializer.

未使用DataMember属性修饰的属性将导致序列化程序忽略它们。

#1


2  

I think you need to indicate the properies on this class to be data members, like so:

我认为你需要指出这个类的特性是数据成员,如下所示:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
  {
    [DataContract]
    public class RegisterModel
    {
        [DataMember]      
        public int UserId { get; set; }
        [DataMember]      
        public string Email { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string ConfirmPassword { get; set; }
        [DataMember]
        public UserDetails UserDetails { get; set; }
    }

}

Same goes for the UserDetails class:

UserDetails类也是如此:

using System.Runtime.Serialization;

namespace APIValueSetterTest.Model
{
    [DataContract]
    public class UserDetails
    {
        [DataMember]
        public int UserId { get; set; }
        [DataMember]
        public string FullName { get; set; }
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string ICEFullName { get; set; }
        [DataMember]
        public int ICEMobileNumber { get; set; }
        [DataMember]
        public string DoctorFullName { get; set; }
        [DataMember]
        public int DoctorMobileNumber { get; set; }
    }
}

Properties not decorated with the DataMember attribute will result in them being ignored by the serializer.

未使用DataMember属性修饰的属性将导致序列化程序忽略它们。