0

I download a json string from a website including a DateTime. Using this code:

using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
    using (Stream responseStream = httpWebResponse.GetResponseStream())
    {
        using (StreamReader streamReader = new StreamReader(responseStream))
        {
            value = streamReader.ReadToEnd();
        }
    }
}
authResponse = JsonConvert.DeserializeObject<Class1.Response>(value);

The Json Class looks like this:

public Response()
{
    this.key = ""
    this.valid_until = "";
    this.xyz = "";
}

The String looks like this:

{
   "key":"1424152",
   "time_date":"2021-09-19 20:35:17",
   "xyz":"Working"
}

Now before I Deserialize it I want to change the Date Time (add 2 days to it) in that string. How would I approach that?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Probably you could do it with a custom datetime converter, check [this related question](https://stackoverflow.com/a/58006576/2265446) – Cleptus Sep 19 '21 at 20:36
  • 8
    Why do you want to do that *before* and not *after* you have deserialized your JSON (so you just need `.AddDays(2)`)? – Jimi Sep 19 '21 at 20:55
  • You may check the [Newtonsoft json serialization events](https://www.newtonsoft.com/json/help/html/SerializationCallbacks.htm) However it creates a DTO object dependency from the serialization framework. The `Response` you have shown is not a class rather a construstor. You may change `valid_until` data type to `DateTime` and a serializer will convert `string` to `dateTime` automatically. Then you may execute `DateTime.AddDays` method at any moment. Does the `valid_until` name differs from the json `time_date` field by mistake or by purpose ? – oleksa Sep 20 '21 at 09:59

0 Answers0