Artem
Artem

Reputation: 3

Trying to create model for json

How to create model for this json? I can't understand how to add dictinary to this string array.

{
  "ts": 1652718271,
  "updates": [
    [
      4,
      508976,
      33,
      466697301,
      1551996353,
      "Цацу",
      {
        "title": " ... ",
        "type": "photo"
      }
    ]
  ]
}

Upvotes: 0

Views: 45

Answers (2)

Nathan Taylor
Nathan Taylor

Reputation: 141

There are a few ways to handle JSON arrays of varied types. One way is to define a class with nullable fields of the types you may encounter in the array. For example,

public class Model
{
    public int TS;
    public Update[][] Updates;
}

public class Update
{
    public int? Number;
    public string Word;
    public ModelDictionary Dictionary;
}

public class ModelDictionary
{
    public string Title;
    public string Type;
}

Then you could access each Update with something like

if (Number != null) { ... }
else if (Word != null) { ... }
else if (Dictionary != null) { ... }

Also, https://app.quicktype.io/ is always a great resource for generating C# models from JSON objects.

Upvotes: 1

Enrique González
Enrique González

Reputation: 180

With this model you can deserialize using Newtonsoft.Json

class Serial 
{
    public string ts { get; set; }
    public object [][] updates { get; set; }

}

Upvotes: 0

Related Questions