Json.net deserializes inconsistent JSON

I work with inconsistent Json that I want to deserialize my .Net class. The problem is that once I have an object, and once it is an array. What is the best way to do this? Converter? Below is a snippet of my Json Data p>

[
{
"@class": "odd",
"td": [
  {
    "@class": "user",
    "@onmouseover": "userInfo('469');",
    "@onmouseout": "userInfo(0);",
    "@onmousemove": "moveSlotInfo();",
    "#text": " AAA"
  },
  {
    "@id": "day-469-2014-04-07",
    "@style": "vertical-align: top;",
    "table": {
      "@class": "ss",
      "@cellspacing": "1",
      "tbody": {
        "tr": {
          "td": {
            "@class": "as",
            "@style": "color: #ffffff; background-color: #4040ff;",
            "@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('177935',false);",
            "@onmouseout": "this.className='as';slotInfo(0,false);",
            "@onmousemove": "moveSlotInfo();",
            "#text": "KAVAul"
          }
        }
      }
    }
  }
]
},
{
"@class": "even",
"td": [
  {
    "@class": "user",
    "@onmouseover": "userInfo('262');",
    "@onmouseout": "userInfo(0);",
    "@onmousemove": "moveSlotInfo();",
    "#text": " BBB"
  },
  {
    "@id": "day-262-2014-04-07",
    "@style": "vertical-align: top;",
    "table": {
      "@class": "ss",
      "@cellspacing": "1",
      "tbody": {
        "tr": [
          {
            "td": {
              "@class": "as",
              "@style": "color: #ffffff; background-color: #4040ff;",
              "@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('174318',false);",
              "@onmouseout": "this.className='as';slotInfo(0,false);",
              "@onmousemove": "moveSlotInfo();",
              "#text": "KAVA "
            }
          },
          {
            "td": {
              "@class": "as",
              "@style": "color: #000000; background-color: #ffc0c0;",
              "@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('174338',false);",
              "@onmouseout": "this.className='as';slotInfo(0,false);",
              "@onmousemove": "moveSlotInfo();",
              "#text": "Dagbak"
            }
          }
        ]
      }
    }
  }
]
}
]

The problem is that the tr object has an object or an array.

Below is a snippet of my class

    public class Td2
{
    [JsonProperty("@class")]
    public string TdClass { get; set; }

    [JsonProperty("@style")]
    public string style { get; set; }

    [JsonProperty("@onmouseover")]
    public string onmouseover { get; set; }

    [JsonProperty("@onmouseout")]
    public string onmouseout { get; set; }

    [JsonProperty("@onmousemove")]
    public string onmousemove { get; set; }

    [JsonProperty("#text")]
    public string text { get; set; }
}

public class Tr2
{
    public Td2 td { get; set; }
}

public class Tbody2
{
    [JsonProperty]
    [JsonConverter(typeof(ScheduleJsonConverter<Tr2>))]
    public List<Tr2> tr { get; set; }
}

public class Table
{
    [JsonProperty("@cellspacing")]
    public string cellspacing { get; set; }

    public Tbody2 tbody { get; set; }

    [JsonProperty("@class")]
    public string tClass { get; set; }
}
+4
source share
1 answer

I got this converter from somewhere, and it was in one of my projects. I would like to pay tribute. I will try to find a message. However, this should solve your problem.

EDIT: AH found this: JSON.NET Deserialize objects in an object / object array

, . :

public class Tbody2
{
     [JsonProperty]
     [JsonConverter(typeof (ObjectToArrayConverter<Tr2>))]
     public List<Tr2> tr { get; set; }
 }

, JSON - , . .

public class ObjectToArrayConverter<T> : CustomCreationConverter<List<T>> where T : new()
{
    public override List<T> Create(Type objectType)
    {
        return new List<T>();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var target = new List<T>();

        try
        {
            // Load JObject from stream
            var jArray = JArray.Load(reader);

            // Populate the object properties
            serializer.Populate(jArray.CreateReader(), target);
        }
        catch (JsonReaderException)
        {
            // Handle case when object is not an array...

            // Load JObject from stream
            var jObject = JObject.Load(reader);

            // Create target object based on JObject
            var t = new T();

            // Populate the object properties
            serializer.Populate(jObject.CreateReader(), t);

            target.Add(t);
        }

        return target;
    }
}
+3

Source: https://habr.com/ru/post/1536357/


All Articles