0

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?

4
  • 1
    It's searching for saleTime property which does not exist in DateTime Commented Nov 14, 2020 at 5:17
  • I think you should create a corresponding class of your JSON txt and deserialize to your class and expose the saleTime by using a method. Commented Nov 14, 2020 at 5:25
  • I'm sorry for this error,and edited question again,the saleTime removed Commented Nov 14, 2020 at 7:52
  • by don't remove saleTime text, I'm also try to create a class by DateTime property,like this public class DateTest { public DateTime saleTime { get; set; } }, and deserialize code modify to this : ` var aa = Newtonsoft.Json.JsonConvert.DeserializeObject<DateTest>(txt); `, the error also exists Commented Nov 14, 2020 at 7:59

2 Answers 2

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

"More specifically the backslashes seem to break the integrity." - the OP's JSON doesn't contain any backslashes. The string literal does, but the JSON doesn't - try using the OP's first line of code and then write Console.WriteLine(txt);. You'll see there are no backslashes.
@JonSkeet Thanks for pointing that out! I ll grab another cup of coffee

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.