Converting a JToken (or string) to a given Type

前端 未结 4 1113
予麋鹿
予麋鹿 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:43

    I was able to convert using below method for my WebAPI:

    [HttpPost]
    public HttpResponseMessage Post(dynamic item) // Passing parameter as dynamic
    {
    JArray itemArray = item["Region"]; // You need to add JSON.NET library
    JObject obj = itemArray[0] as JObject;  // Converting from JArray to JObject
    Region objRegion = obj.ToObject(); // Converting to Region object
    }
    

提交回复
热议问题