Search code examples
c#jsonjson.netjson-deserialization

Deserialization malformed JSON with custom constructor


Is there any way to deserialize this malformed JSON code?

{
"children": [
{
  "bold": false,
  "italic": false,
  "strike": false,
  "underline": false,
  "link": false
},
"Hello world" ]
}

JsonSerializerOptions has the option of a custom Converters.

Would anyone know how to show me the way?

Thank you.


Solution

  • the simpliest way to deserialize your json is

    var data = JsonConvert.DeserializeObject<Data>(json);
    
    public class Data
    {
        public List<object> children { get; set; }
    }
    

    how to use

    string helloWorld=data.children[1].ToString();
    
    Dictionary<string,bool> fonts= ((JObject) data.children[0]).ToObject<Dictionary<string,bool>>();