skip to Main Content

This is a section of my JSON string file.

{
  "Abbrev": "ALEX",
  "MeetingDate": "/Date(1679058000000+1100)/",

This is the class creation

public class PFGetForm
    {
        public string Abbrev { get; set; }        
        public DateTime MeetingDate { get; set; }   

I am trying to add the Meeting Date to a list

line.Add($"{form.MeetingDate.ToString("dd/MM/yy")}");

The error message is:

System.Text.Json.JsonException: 'The JSON value could not be converted to System.DateTime. Path: $.MeetingDate | LineNumber: 0 | BytePositionInLine: 61.'

FormatException: The JSON value is not in a supported DateTime format.

Trying to parse the MeetingDate field from the JSON file and add it to a list so I can then send it all to a csv file.

2

Answers


  1. Set your property as string:

    public class PFGetForm
        {
            public string Abbrev { get; set; }        
            public string MeetingDate { get; set; }   
    } 
    

    and then convert it to timestamp. Demo:

        string MeetingDate  = "1679058000000";
        DateTime timestamp = DateTime.UnixEpoch.AddMilliseconds(Convert.ToInt64(MeetingDate));
    

    Fiddle:

    https://dotnetfiddle.net/puJYxm

    Login or Signup to reply.
  2. I highly recommed you to use Newtonsoft.Json. Otherwise you will not have any time for programming, since you will be very busy creating custom converters for Text.Json .

     PFGetForm form = JsonConvert.DeserializeObject<PFGetForm>(json);
    
    string date = form.MeetingDate.ToString("dd/MM/yy"); // "17-03-23"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search