0

I am using Newtonsoft.Json in my project for json parsing from server.

public class MyObj
{
    public DateTimeOffset TimeStamp { get; set; }
    //other fields....
}

Then:

MyObj test = JsonConvert.DeserializeObject<MyObj>(jObject.ToString());

Test:

"TimeStamp": "2018-05-26T04:59:40:888Z" //Could not convert string to DateTimeOffset
"TimeStamp": "2018-05-26T04:59:40:88Z"  //Could not convert string to DateTimeOffset
"TimeStamp": "2018-05-26T14:59:40:888Z" //Could not convert string to DateTimeOffset
"TimeStamp": "2018-05-26T14:59:40:88Z"  //Could not convert string to DateTimeOffset

"TimeStamp": "2018-05-26T03:29:46.777Z" //works
"TimeStamp": "2018-05-26T13:29:46.77Z"  //works
"TimeStamp": "2018-05-26T03:29:46.777Z" //works
"TimeStamp": "2018-05-26T13:29:46.77Z"  //works

Error:

Newtonsoft.Json.JsonReaderException: Could not convert string to DateTimeOffset: 2018-05-27T04:59:40:887Z.

I am not sure why this happens, because the date is from server.

Edit:

{
  "clientTimestamp": "2018-05-27T06:08:40:688Z",
  "modifiedType": "",
  "type": "TEXT",
  "messageSize": 5,
  "roomId": "689355a0-604b-11e8-ae6a-9d170520ec46",
  "messageContent": "hello"
}

Update I finally found the issue. It was not the server response that I was parsing. It was my own object that I parsed. The description:

public class TempClass
{
    public DateTime TimeStamp { get; set; }
}

Does not work

JObject jObject = new JObject();
jObject.Add("TimeStamp", DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss:fffZ"));
TempClass chatMessage = JsonConvert.DeserializeObject<TempClass>(jObject.ToString());

Works

JObject jObject = new JObject();
jObject.Add("TimeStamp", DateTime.Now);
TempClass chatMessage = JsonConvert.DeserializeObject<TempClass>(jObject.ToString());
11
  • 2
    Can you use DateTime instead of DateTimeOffset ? Commented May 27, 2018 at 0:07
  • @Tony I think same thing happen with DateTime. Any idea why it happens? Commented May 27, 2018 at 0:09
  • 2
    GIve a try with DateTime. I am using it right now and it works fine with Json.Net Commented May 27, 2018 at 0:10
  • If you want something more complex, see also: stackoverflow.com/a/23505631/194717 Commented May 27, 2018 at 0:12
  • @Tony I appreciate your help but it still showing that error Could not convert string to DateTime: 2018-05-27T05:49:09:714Z Commented May 27, 2018 at 0:20

2 Answers 2

3

Your timestamp is incorrect

Instead of 2018-05-27T06:08:40:688Z should be 2018-05-27T06:08:40.688Z
(the millisecond is separated by a dot . )

Try this

public class RootObject
{
    public DateTime clientTimestamp { get; set; }
    public string modifiedType { get; set; }
    public string type { get; set; }
    public long messageSize { get; set; }
    public Guid roomId { get; set; }
    public string messageContent { get; set; }
}

Then:

MyObj test = JsonConvert.DeserializeObject<RootObject>(jObject.ToString());

In fact

2018-05-27T06:08:40:688Z
Could not convert string to DateTime: 2018-05-27T06:08:40.688Z

2018-05-27T06:08:40.688Z
OK

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I got it. Thank you so much for your help
Use DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
I have been banging my head for a long time. Thank you
0

It seems that this is happening because data from your server is not sent in the correct json format for the Date/Time, and you are trying to deserialize them.

2 Comments

No it is sending because I can see it. The problem is with the date field, but I am not able to find it out.
Add the Json content to the question so we can help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.