0

So I have to send a request to a server to receive some Json and convert it into a class to use that data later. I'm working in Unity and for the deserialization I'm using Newtonsoft json.net.

Now the problem is with a value I receive from the server and that it won't deserialize in the correct variable. It's an expiration date that is send like this: ".expires": "2000-01-01T00:00:00.0000000+02:00".

I'm not yet sure if it is able to convert the 2000-01-01T00:00:00.0000000+02:00 into a DateTime or not, but that's a problem for later. The main problem is that I can't create a variable in C# named .expires (because of the .). But it seems that Json.NET won't recognize that .expires should be converted into expires. Does anyone know how to change this in the settings or something simular?

(It's not possible to just remove all '.' from the response string since that character can be and is used as part of a value.)

Guru Stron
  • 102,774
  • 10
  • 95
  • 132

1 Answers1

2

Your model should have JsonProperty. The DateTime property will take care of necessary conversions

public class xxx
{
    [JsonProperty(".expires")]
    public DateTime Expires { get; set; }
}

Usage

var json = "{\".expires\": \"2000-01-01T00:00:00.0000000+02:00\"}";
var result = JsonConvert.DeserializeObject<xxx>(json);
Console.WriteLine(result.Expires.ToString());

Output

31.12.1999 23:00:00
Krishna Varma
  • 4,238
  • 2
  • 10
  • 25