c# - 如何在类属性中分配值类

标签 c# winforms

<分区>

我最近的一位开发人员为我提供了解决方案。创建类但是如何为这个类赋值..请帮助我

创建一个具有要在请求中发送的所需属性的类。 //例如

   public class BookingInformation
    {
       public string booking_id { get; set; }
       public ToLocation to_location { get; set; }
       public string notes { get; set; }
    }



   public class ToLocation
 {
     public double latitude { get; set; }
     public double longitude { get; set; }
     public Address2 address { get; set; }
     public object comment { get; set; }
     public object airport { get; set; }
 }
 public class Address2
 {
     public string display_address { get; set; }
     public string building_number { get; set; }
     public string street_name { get; set; }
     public string city { get; set; }
     public string region { get; set; }
     public string postal_code { get; set; }
     public string country { get; set; }
 }

给对象赋值

Request request =new Request();
request.vehicles  = new List<Vehicle>();

//等等

//Use Newtonsoft.Json to serialize the object as:
var json = JsonConvert.SerializeObject(request);
//Invoke your request with json
using (var response = await httpClient.PostAsync("{supplier_id}/availability?version=2", json))
{
    string responseData = await response.Content.ReadAsStringAsync();
}

但是我的方法显示错误

 public void ConvertJson()
         {
             BookingInformation objbooking = new BookingInformation();
             objbooking.booking_id = "33";
             objbooking.to_location.comment= "Saloon black";
             objbooking.to_location.address.country= "UK";
             var json = JsonConvert.SerializeObject(objbooking);
         }

enter image description here

最佳答案

     public void ConvertJson()
     {
         BookingInformation objbooking = new BookingInformation();
         objbooking.booking_id = "33";
         objbooking.to_location.comment= "Saloon black";
         objbooking.to_location.address.country= "UK";
         var json = JsonConvert.SerializeObject(objbooking);
     }

您还没有对 ToLocation 实例的引用。更改为:

     public void ConvertJson()
     {
         BookingInformation objbooking = new BookingInformation();
         objbooking.to_location = new ToLocation();
         objbooking.to_location.address = new Address2();
         objbooking.booking_id = "33";
         objbooking.to_location.comment= "Saloon black";
         objbooking.to_location.address.country= "UK";
         var json = JsonConvert.SerializeObject(objbooking);
     }

根据您的编辑,您还没有 Address2 的实例,我也更新了上面的内容以说明这一点。

另请通读以下回复:What is a NullReferenceException, and how do I fix it?有关 NullReferenceExceptions 如何/为何发生的更多信息。

另一种解决这个问题的方法是从父类的构造开始,这样您就不必记住更新需要它的对象的属性,如下所示:

public class BookingInformation
{
    public string booking_id { get; set; }
    public ToLocation to_location { get; set; }
    public string notes { get; set; }

    public BookingInformation()
    {
        to_location = new ToLocation();
    }
}

public class ToLocation
{
    public double latitude { get; set; }
    public double longitude { get; set; }
    public Address2 address { get; set; }
    public object comment { get; set; }
    public object airport { get; set; }

    public ToLocation()
    {
        address = new Address2();
    }
}

这将确保当您创建 BookingInformation 的新实例时,ToLocation 又会在构造时创建 Address2 实例。

关于c# - 如何在类属性中分配值类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186362/

相关文章:

c# - 使用 JSON.net 反序列化为对象时从时间戳中丢失毫秒数

c# - 如何在asp.net core中以强类型方式获取资源字符串?

c# - 关闭的 XML 根据单元格值更改整行背景颜色

vb.net - 根据键数组检查 e.KeyChar

c# - 为什么单击文本框会导致 AutoScroll 面板滚动回顶部?

.net - 如何取消选择 DataGridView 控件中的所有选定行?

c# - ORA-01008 与所有变量绑定(bind)

c# - 带有 Action 参数的最小起订量

.net - 创建新 GUI 时,WPF 是不是 Windows 窗体的首选?

c# - 使用模态对话框的 WinForms 应用程序中的异常处理行为