2

I want to create class to deserialize following JSON. I have tried with http://json2csharp.com/ but it doesn't work.

JSON

{
    "usertoken": "TqasdfgdSz0UleD-hdST5sigIE1MdOlKQ2HXrcvTwtCpveN9Fm",
    "expires_sec": 12095,
    "user": "testuser.com",
    ".expired": "Sat, 2 Jun 2018 09:25:00 GMT"
}

Tried with following class but issue in .expired class member

public class Authentication
{
    public string usertoken { get; set; }
    public int expires_sec { get; set; }
    public string user { get; set; }
    public string .expired { get; set; }    //.will not work here
}

Please suggest

Developer
  • 295
  • 4
  • 16
  • 1
    Improvement suggestion for your next question: "Doesn't work" is not a very good problem description. In your case, "doesn't compile with the following error message: ..." (or: "doesn't compile because C# identifiers cannot contain a dot", if you already know that) would be much better. Otherwise: Good question, good title! – Heinzi Jun 01 '18 at 06:58
  • This may help you. [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Ved Prakash Tiwari Jun 01 '18 at 07:02

1 Answers1

4

You are on right track. All you need to use is JsonProperty. It will help you to define the property name that your program will use and name that your json string will use during serialization and deserialization operations.

Check following sample.

public class Authentication
{
    public string usertoken { get; set; }
    public int expires_sec { get; set; }
    public string user { get; set; }
    [JsonProperty (".expired")]
    public string expired { get; set; }
}

You need to install Newtonsoft.Json nuget package to achieve this.

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34