Converting a JToken (or string) to a given Type

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

    var i2 = JsonConvert.DeserializeObject(obj["id"].ToString(), type);
    

    throws a parsing exception due to missing quotes around the first argument (I think). I got it to work by adding the quotes:

    var i2 = JsonConvert.DeserializeObject("\"" + obj["id"].ToString() + "\"", type);
    

提交回复
热议问题