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

时间:2016-10-11 17:51:41

标签: c# .net json datacontractjsonserializer

我正在制作一个旅行应用项目,包括用PHP编码的后端和用C#编码的UWP应用(前端)。

以下代表用C#实现的“假日套餐”类

class Panel extends React.Component {

  render() {
    return (
      <div>
        <div>{this.props.title}</div>
        <div>{this.renderBody()}</div>
        <div><button>Submit</button></div>
      </div>
    )
  }

}

class SomeSubPanel extends Panel {

  constructor(props) {
    // React throws the error message at the following line
    let newProps = Object.assign({}, props, {title: "Some Sub Panel"})
    super(newProps)
  }

  renderBody() {
    return (<div>Panel Body Goes Here</div>)
  }

}

以下是后端返回的JSON字符串

 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字符串反序列化为“Packages”类,从而设置其“PackageID”,“Name”,“Destination”,“Description”,“Duration”和“BasePrice”属性,即我只想设置使用Web数据的属性子集

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

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

1 个答案:

答案 0 :(得分:0)

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

这是有效的,因为对于数据协定类型,数据合同序列化程序does not call any constructor at all

Packages(可能还有HotelInPackage(未包含在问题中)实施了这些选项之一,您现在可以反序列化您的JSON。只会设置JSON中实际存在的那些属性。