简体   繁体   English

Json数据中的反序列化返回空值

[英]Deserialization in Json data returns null values

I am working in a windows 10 Universal app, while trying to receive data from my webAPI, my code is as follows: 我正在Windows 10 Universal应用程序中工作,尝试从WebAPI接收数据时,我的代码如下:

        try
        {
            string _serviceUrl = Constants.BaseUrl + "api/RegisterBindingModels?email=" + Useremail;

            HttpClient client = new HttpClient();

            HttpResponseMessage responce = await client.GetAsync(new Uri(_serviceUrl));

            if (responce.Content != null)
            {
                var obj = await responce.Content.ReadAsStringAsync();


                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.MissingMemberHandling = MissingMemberHandling.Ignore;
                var rcvdData = JsonConvert.DeserializeObject<RegisterModel>(obj, settings);
            }
        }
        catch (Exception)
        {

            throw;
        }

while this code runs, the Obj gets the following JSON, which is as expected: 在运行此代码时,Obj将获得以下JSON,这与预期的一样:

 { "UserDetails":{ "UserId":1, "FullName":"sample string 2", "Username":"sample string 3", "ICEFullName":"sample string 4", "ICEMobileNumber":5, "DoctorFullName":"sample string 6", "DoctorMobileNumber":7 }, "UserId":1, "Email":"akshay@gmail.com", "Password":"add‌​sFABBS!2", "ConfirmPassword":"addsFABBS!2" } 

Nevertheless the var rcvdData has all null values: 不过,var rcvdData具有所有空值:

像这样

The RegisterModel is as follows: RegisterModel如下:

namespace APIValueSetterTest.Model
{
    using System.Runtime.Serialization;

    [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 UserDetails Model is as follows: 而UserDetails模型如下:

namespace APIValueSetterTest.Model
{
    using System.Runtime.Serialization;

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

I need help, where am I going wrong? 我需要帮助,我哪里出问题了?

The problem is that you have marked your types with [DataContract] but you have not marked each member to be serialized with [DataMember] . 问题是您已经用[DataContract]标记了类型,但是还没有使用[DataMember]标记要序列化的每个成员。 Explicit data contract serialization is opt-in, as is explained in Using Data Contracts : 显式数据合同序列化是可选的,如使用数据合同中所述

You can also explicitly create a data contract by using DataContractAttribute and DataMemberAttribute attributes. 您还可以使用DataContractAttributeDataMemberAttribute属性显式创建数据协定。 This is normally done by applying the DataContractAttribute attribute to the type. 通常通过将DataContractAttribute属性应用于该类型来完成此操作。 This attribute can be applied to classes, structures, and enumerations. 此属性可以应用于类,结构和枚举。 The DataMemberAttribute attribute must then be applied to each member of the data contract type to indicate that it is a data member , that is, it should be serialized . 然后,必须将DataMemberAttribute属性应用于数据协定类型的每个成员,以表明它是数据成员 ,即应进行序列化 For more information, see Serializable Types . 有关更多信息,请参见可序列化类型

As supports data contracts, as explained in its docs , you need to remove [DataContract] , or add [DataMember] to all serializable members. 由于支持数据协定(如其文档所述) ,因此您需要删除[DataContract]或向所有可序列化成员添加[DataMember]

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

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