Deserializing an unknown type in JSON.NET

后端 未结 3 1884
我寻月下人不归
我寻月下人不归 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:11

    it may help you

    IDictionary < string, JToken > Jsondata = JObject.Parse(yourJsonString);
       foreach(KeyValuePair < string, JToken > element in Jsondata)
        {
               string innerKey = element.Key;
                if (element.Value is JArray)
                 {
                      // Process JArray
                 }
                else if (element.Value is JObject) 
                { 
                      // Process JObject
                }
       }
    
    
    0 讨论(0)
  • 2021-01-11 12:15

    you can use dynamic type

    JsonConvert.DeserializeObject<dynamic>(JSONtext)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题