Deserializing an unknown type in JSON.NET

后端 未结 3 1883
我寻月下人不归
我寻月下人不归 2021-01-11 11:38

I just got a hold of JSON.NET and its been great so far.

However, I cannot figure out how to determine the type of a serialized object when

3条回答
  •  不知归路
    2021-01-11 12:15

    In case you control the serialization, you can use the TypeNameHandling setting

    var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
    var toBeSerialized = settings; // use the settings as an example data to be serialized
    
    var serialized = JsonConvert.SerializeObject(toBeSerialized, Formatting.Indented, settings);
    var deserialized = JsonConvert.DeserializeObject(serialized, settings);
    
    var deserializedType = deserialized.GetType().Name; // JsonSerializerSettings
    

提交回复
热议问题