如何使用DataContractJsonSerializer使用从web(以JSON形式)获得的数据设置对象的属性子集

本文关键字:数据 设置 子集 属性 对象 形式 JSON DataContractJsonSerializer 何使用 web | 更新日期: 2023-09-27 18:12:32

我正在制作一个旅游应用程序项目,由PHP编码的后端和c#编码的UWP应用程序(前端)组成。

下面是c#中实现的"Holiday Package"类

 public class Packages
{
    public string PackageID { get; set; }
    public string Name { get; set; }
    public string Destination { get; set; }
    public string Description { get; set; }
    public int Duration { get; set; }
    public float BasePrice { get; set; }
    public List<string> Images { get; set; }
    public HotelInPackage Hotel { get; set; }
    public string TransportType { get; set; }
    public Packages(string packageID,string name,string destination,string description,int duration,float basePrice,List<string> images)
    {
        PackageID = packageID;
        Name = name;
        Destination = destination;
        Description = description;
        Duration = duration;
        BasePrice = basePrice;
        Images = images;
    }
    public void HotelConstruct(string hotelID,string name,int cat)
    {
        Hotel = new HotelInPackage(hotelID, name, cat);
    }
    public void SetTransport(string transportType)
    {
        TransportType = transportType;
    }
    public void ChangeImageName()
    {
        int i = 0;
        while(i<Images.Count)
        {
            Images[i] = string.Format("Assets/CitiesPlaceholder/{0}.jpg",Images[i]);
            i++;
        }
    }
}

以下是后端

返回的JSON字符串
{
"PackageID":"P280",
"Name":"Sigapore Dreams",
"Destination":"Singapore",
"Description":"lorem ipsum,dolor sit amet",
"Duration":5,
"BasePrice":999.2
}

我想将上述JSON字符串反序列化到"Packages"类中,从而设置其"PackageID","Name","Destination","Description","Duration"answers"BasePrice"属性,即我想只设置使用web数据的属性子集

如何使用DataContractJsonSerializer类实现上述解决方案?

我需要添加/修改任何构造函数吗?

如何使用DataContractJsonSerializer使用从web(以JSON形式)获得的数据设置对象的属性子集

DataContractJsonSerializer永远不会调用参数化构造函数。因此,由于您的Packages类型缺少无参数构造函数,因此它抛出异常,因为它不知道如何构造这种类型的实例。

你有两种方法来启用DataContractJsonSerializer来构造你的对象。首先,您可以添加一个无参数构造函数。它甚至可以是私有的:

public class Packages
{
    public string PackageID { get; set; }
    public string Name { get; set; }
    public string Destination { get; set; }
    public string Description { get; set; }
    public int Duration { get; set; }
    public float BasePrice { get; set; }
    public List<string> Images { get; set; }
    public HotelInPackage Hotel { get; set; }
    public string TransportType { get; set; }
    Packages()
    {
        Debug.WriteLine("Calling private constructor of " + GetType().FullName);
    }
    public Packages(string packageID, string name, string destination, string description, int duration, float basePrice, List<string> images)
    {
        PackageID = packageID;
        Name = name;
        Destination = destination;
        Description = description;
        Duration = duration;
        BasePrice = basePrice;
        Images = images;
    }
}

或者,如果您甚至不想要一个私有的无参数构造函数,您可以用[DataContract][DataMember]属性标记您的类型:

[DataContract]
public class Packages
{
    [DataMember]
    public string PackageID { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Destination { get; set; }
    [DataMember]
    public string Description { get; set; }
    [DataMember]
    public int Duration { get; set; }
    [DataMember]
    public float BasePrice { get; set; }
    [DataMember]
    public List<string> Images { get; set; }
    [DataMember]
    public HotelInPackage Hotel { get; set; }
    [DataMember]
    public string TransportType { get; set; }
    public Packages(string packageID, string name, string destination, string description, int duration, float basePrice, List<string> images)
    {
        PackageID = packageID;
        Name = name;
        Destination = destination;
        Description = description;
        Duration = duration;
        BasePrice = basePrice;
        Images = images;
    }
}

这是有效的,因为对于数据契约类型,数据契约序列化器根本不调用任何构造函数。

在为Packages(可能还有HotelInPackage,问题中没有包括)实现了这些选项中的任何一个之后,现在可以反序列化JSON了。只有JSON中实际存在的属性才会被设置。