2

my recent post one developer provide me solution. create class but how to assign values this class.. please help me

Create a class with required properties to be sent in request. //for example

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

Assign the values to the object

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

// and so on

//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();
}

but my method show error

 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

Adeel Khan
  • 175
  • 1
  • 13

2 Answers2

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

You do not yet have a reference to an instance of ToLocation. Change to this:

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

Based on your edits, you would also not yet have an instance of Address2, I updated the above to account for that as well.

Also read through the responses in: What is a NullReferenceException, and how do I fix it? for more information on how/why NullReferenceExceptions occur.

Another method of getting around this problem, so you don't have to remember to new up properties of the object that require it, is to do it from construction of the parent, like so:

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

This will ensure when you create a new instance of BookingInformation the ToLocation and in turn Address2 have instances created at construction.

Community
  • 1
  • 1
Kritner
  • 12,693
  • 10
  • 45
  • 68
  • i have edit my post thanks for answer me.. please provide above post solution – Adeel Khan May 12 '16 at 12:16
  • @AdeelKhan, he provided a solution. His above code is the code you're using, then the below code contains his additions. – Rik P May 12 '16 at 12:19
1

first create an instance of class ToLocation like this :

  objbooking.to_location = new ToLocation();