Deserializing json to anonymous object in c#

前端 未结 5 1542
深忆病人
深忆病人 2020-12-11 00:56

How do I convert a string of json formatted data into an anonymous object?

5条回答
  •  鱼传尺愫
    2020-12-11 01:07

    using Newtonsoft.Json, use DeserializeAnonymousType:

    string json = GetJsonString();
    var anonType = new { Order = new Order(), Account = new Account() };
    var anonTypeList = new []{ anonType }.ToList(); //Trick if you have a list of anonType
    var jsonResponse = JsonConvert.DeserializeAnonymousType(json, anonTypeList);
    

    Based my answer off of this answer: https://stackoverflow.com/a/4980689/1440321

提交回复
热议问题