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());
DateTimeinstead ofDateTimeOffset?DateTime. I am using it right now and it works fine with Json.NetCould not convert string to DateTime: 2018-05-27T05:49:09:714Z