2016-05-12 43 views
2

我最近發佈的一篇開發人員爲我提供瞭解決方案。創建類,但如何分配這個類的價值..請幫助我如何在類屬性中指定值類

創建一個具有所需的屬性的類,在請求發送。 //例如

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

+2

有什麼錯誤? – RhysO

+0

我編輯我的帖子.. –

+1

其中是'booking_id'屬性定義類 –

回答

2
 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在構建時創建實例。

+0

我編輯我的帖子感謝您回答我..請提供上述帖子解決方案 –

+0

@AdeelKhan,他提供了一個解決方案。他的上面的代碼是你正在使用的代碼,然後下面的代碼包含他的增加。 –

+0

謝謝親愛的.... –

1

首先創建一個這樣的類ToLocation的一個實例:

objbooking.to_location = new ToLocation(); 
相關問題