Converting a JToken (or string) to a given Type

前端 未结 4 1118
予麋鹿
予麋鹿 2020-12-08 09:00

TL;DR Version

I have a object of type JToken (but can also be a string) and I need to convert it into a Type contained in

4条回答
  •  心在旅途
    2020-12-08 09:48

    There is a ToObject method now.

    var obj = jsonObject["date_joined"];
    var result = obj.ToObject();
    

    It also works with any complex type, and obey to JsonPropertyAttribute rules

    var result = obj.ToObject();
    
    public class MyClass 
    { 
        [JsonProperty("date_field")]
        public DateTime MyDate {get;set;}
    }
    

提交回复
热议问题