Deserialize json object into dynamic object using Json.net

后端 未结 8 1739
感情败类
感情败类 2020-11-21 22:07

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this:

dynamic jsonResponse = JsonConv         


        
8条回答
  •  梦毁少年i
    2020-11-21 22:35

    Note: At the time I answered this question in 2010, there was no way to deserialize without some sort of type, this allowed you to deserialize without having go define the actual class and allowed an anonymous class to be used to do the deserialization.


    You need to have some sort of type to deserialize to. You could do something along the lines of:

    var product = new { Name = "", Price = 0 };
    dynamic jsonResponse = JsonConvert.Deserialize(json, product.GetType());

    My answer is based on a solution for .NET 4.0's build in JSON serializer. Link to deserialize to anonymous types is here:

    http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx

提交回复
热议问题