The JSON is:
{"date":13,"day":5,"hours":19,"minutes":6,"month":10,"nanos":0,"seconds":41,"time":1605265601000,"timezoneOffset":-480,"year":120}
When I try to Convert to DateTime
, I encountered the following error:
Newtonsoft.Json.JsonReaderException HResult=0x80131500 Message=Unexpected character encountered while parsing value: {. Path '', line 1, position 1. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType) at Newtonsoft.Json.JsonTextReader.ReadAsDateTime() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at test.Program.Main(String[] args) in E:\code\UI\test\Program.cs:line 77
My Code:
var txt = "{\"date\":13,\"day\":5,\"hours\":19,\"minutes\":6,\"month\":10,\"nanos\":0,\"seconds\":41,\"time\":1605265601000,\"timezoneOffset\":-480,\"year\":120}";
var aa = Newtonsoft.Json.JsonConvert.DeserializeObject<DateTime>(txt);
Console.ReadKey();
when I use Newtonsoft.Json.dll 3.5 version instead, the error disappears. when I use Newtonsoft.Json.dll 9.0 version instead, the error appears.
I'm using VS2017 to Build, where is my Error?
I suspect Json.NET now expects DateTime
values to be represented as strings, rather than as JSON objects. Certainly the representation you've got is an unusual one. I suggest you create your own class that follows the JSON, and write a ToDateTime
(or better ToDateTimeOffset
) to convert it.
(I'm really surprised that earlier versions of Json.NET could handle this at all.)
Better yet, if you're in control of the code that's creating this JSON, ideally change it to a more sensible format, e.g. an ISO-8601 string.
Here's an example of code to perform the conversion. The offset handling is a little odd, due to a mixture of the way DateTimeOffset
works and the way the offset appears to be expressed :(
using System;
using Newtonsoft.Json;
class Test
{
static void Main()
{
var txt = "{\"date\":13,\"day\":5,\"hours\":19,\"minutes\":6,\"month\":10,\"nanos\":0,\"seconds\":41,\"time\":1605265601000,\"timezoneOffset\":-480,\"year\":120}";
var jdto = JsonConvert.DeserializeObject<JsonDateTimeOffset>(txt);
var dto = jdto.ToDateTimeOffset();
Console.WriteLine(dto);
Console.WriteLine(dto.ToUniversalTime());
}
}
public class JsonDateTimeOffset
{
// All ignored as the Time property handles all these.
public int Date { get; set; }
public int Day { get; set; }
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
// Ignored, as we don't know what it means.
public int Nanos { get; set; }
// Milliseconds since the unix epoch
public long Time { get; set; }
// UTC offset in minutes, but reversed from the normal
// view.
[JsonProperty("timezoneOffset")]
public int TimeZoneOffset { get; set; }
public DateTimeOffset ToDateTimeOffset()
{
var instant = DateTimeOffset.FromUnixTimeMilliseconds(Time);
var offset = -TimeSpan.FromMinutes(TimeZoneOffset);
return new DateTimeOffset(instant.DateTime + offset, offset);
}
}
Your deserialised object is actually
public class Rootobject
{
public int date { get; set; }
public int day { get; set; }
public int hours { get; set; }
public int minutes { get; set; }
public int month { get; set; }
public int nanos { get; set; }
public int seconds { get; set; }
public long time { get; set; }
public int timezoneOffset { get; set; }
public int year { get; set; }
}
and you cannot deserialize this class directly to DateTime.
Now two options come to my mind.
Use an custom converter or create a new DateTime
instance from the properties of the RootObject
class.
User contributions licensed under CC BY-SA 3.0