toobject ()扩展方法将存储为字符串的datetime值转换。

时间:2022-11-23 16:41:02

When calling ToObject on JObject with a string property, transforms datetime value.

在使用字符串属性的JObject调用ToObject时,将转换datetime值。

class Program
{
    static void Main(string[] args)
    {
        var a = JObject.Parse("{\"aprop\":\"2012-12-02T23:03:31Z\"}");

        var jobject = a.ToObject<A>();
        Console.ReadKey();
    }
}

public class A
{
    public string AProp { get; set; }
}

The problem is I get my value transformed despite it being a string. The ISO8601 specific characters got skipped:

问题是,尽管它是一个字符串,但我的值被转换了。ISO8601特定字符被跳过:

toobject ()扩展方法将存储为字符串的datetime值转换。

I expect no tranformations to happen and want to be able to do date validation and culture-specific creation myself. I also tried the next code without success:

我希望不会发生任何转换,并且希望能够自己完成日期验证和特定文化的创建。我还尝试了下一个没有成功的代码:

var jobject = a.ToObject<A>(new JsonSerializer
    {
        DateParseHandling = DateParseHandling.None
    });

The JObject.Parse is introduced for example's sake. In my real task I have a Web.Api action on a controller:

JObject。例如,Parse是引入的。在我真正的任务中,我有一个网络。控制器上的Api操作:

public HttpResponseMessage Put(JObject[] requestData)
{  
    var jobject = a.ToObject<A>();
    return SomeCleverStaffResponse();
}

2 个解决方案

#1


4  

what you want is

你想要的是

using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        var temp = JsonConvert.DeserializeObject<A>("{\"aprop\":\"2012-12-02T23:03:31Z\"}");
        Console.ReadKey();
    }
}

as soon as you do Parse since "2012-12-02T23:03:31Z\" is a date the parser creates a Date Object everything after that will already have the object parsed so the .ToObject is useless as what you are doing is going from date to string and that's why you get the "12/...".

从“2012-12-02T23:03:31Z\”开始解析之后,解析器就会创建一个date对象,在此之后,这个对象就会被解析,所以. toobject是无用的,因为你所做的是从日期到字符串,这就是为什么你得到了“12/…”。

#2


0  

Why do you parse it, when you don't want to have it parsed, at the first place? There's no reason for building a JObject instance. Just deserialize the string using the JsonSerializer.Deserialize<T> method directly into an A instance. You can then leverage all Json.NET's attributes (among other means) to control deserialization as you want.

你为什么要解析它,当你不想让它被解析的时候?没有理由构建一个JObject实例。使用JsonSerializer对字符串进行反序列化。将 方法直接反序列化为一个实例。然后可以利用所有Json。NET的属性(包括其他方法)来控制反序列化。

#1


4  

what you want is

你想要的是

using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        var temp = JsonConvert.DeserializeObject<A>("{\"aprop\":\"2012-12-02T23:03:31Z\"}");
        Console.ReadKey();
    }
}

as soon as you do Parse since "2012-12-02T23:03:31Z\" is a date the parser creates a Date Object everything after that will already have the object parsed so the .ToObject is useless as what you are doing is going from date to string and that's why you get the "12/...".

从“2012-12-02T23:03:31Z\”开始解析之后,解析器就会创建一个date对象,在此之后,这个对象就会被解析,所以. toobject是无用的,因为你所做的是从日期到字符串,这就是为什么你得到了“12/…”。

#2


0  

Why do you parse it, when you don't want to have it parsed, at the first place? There's no reason for building a JObject instance. Just deserialize the string using the JsonSerializer.Deserialize<T> method directly into an A instance. You can then leverage all Json.NET's attributes (among other means) to control deserialization as you want.

你为什么要解析它,当你不想让它被解析的时候?没有理由构建一个JObject实例。使用JsonSerializer对字符串进行反序列化。将 方法直接反序列化为一个实例。然后可以利用所有Json。NET的属性(包括其他方法)来控制反序列化。