1

trying to get a list of dates, I need to add 2 strings in between the URL

  1. Cant concatenate the URL right
  2. Need help deserializing into a DateTime list

    public async Task<List<DateTime>>GetDate()
    {
     // Original Url  http://local:8796550/serv/newobj/v678/object/35b724c5424/serv/addIDinhere/dates .    
        List<DateTime> dates = new List<DateTime>();
    
        var Id= "xxxxxxxxxxxxxxhola57a";
        var Uri = "http://local:8796550/serv/newobj/v678/object/35b724c5424/serv/";
        var Date="/dates";
        var client = new HttpClient();
        var ServicesDateRequest = await client.GetAsync($"{Uri}&={Id}&={Date}");
        string oj = await ServicesDateRequest.Content.ReadAsStringAsync();
        //here deserialized it into datetime list 
        var DatesJson = JsonConvert.DeserializeObject<dates>(oj);
    
      return dates;
    
    }
    
Joshua Robinson
  • 3,399
  • 7
  • 22
Pxaml
  • 651
  • 2
  • 12
  • 38

2 Answers2

1

When you're using string interpolation you only need the curly braces and the expression. The "&=" that you're adding between each component is unnecessary to get to the url format you're looking for (assuming that the format you want is what the "Original Url" comment shows).

var ServicesDateRequest = await client.GetAsync($"{Uri}{Id}{Date}");

For deserialization, check out the documentation on the method you're using. Specifically, the part about Type Parameters. JsonConvert.DeserializeObject<T>(string).

Type Parameters T The type of the object to deserialize to.

In other words, you should be using the type that you want to deserialize (in this case List<DateTime> rather than the name of a variable.

Joshua Robinson
  • 3,399
  • 7
  • 22
0

Try this. You can use string.format to format your url with query string paramteres. Do Deserialize, you need to use type no data.

public async Task<List<DateTime>> GetDate()
    {
        // Original Url  http://local:8796550/serv/newobj/v678/object/35b724c5424/serv/addIDinhere/dates .    
        List<DateTime> dates = new List<DateTime>();
        var Id = "xxxxxxxxxxxxxxhola57a";
        var Date = "/dates";
        var client = new HttpClient();
        var formatedUrl = string.Format("http://local:8796550/serv/newobj/v678/object/35b724c5424/serv/?id={0}&date={1}", Id, Date);
        var ServicesDateRequest = await client.GetAsync(formatedUrl);
        string oj = await ServicesDateRequest.Content.ReadAsStringAsync();
        //here deserialized it into datetime list 
        var DatesJson = JsonConvert.DeserializeObject<DateTime>(oj);

        return DatesJson;

    }