Home > Enterprise >  Deserialize array of array using json .net
Deserialize array of array using json .net

Time:03-20

I have a json that looks like so:

[
["0",true,"90","0","1647537980","0","243729846566","105591923388",false,["0","0","0"],[false,"0","0","0"]],["1",true,"42","0","1646708581","1646708581","111003905","0",false,["156","94348800","1646426440"],[false,"135238559235","14754525","4"]],
["2",true,"20","0","1646708602","1646708602","54061667","0",false,["52","31449600","1646538934"],[false,"2031490329","223870","2"]],
]

How would one go about deserializing this json?

I tried adding [JsonConstructor] on the MyModel constructor, but it never gets called.

var obj = JsonSerializer.Deserialize<List<MyModel>>(json);

Thanks!

CodePudding user response:

you have a double list , so or fix json or use this code

var obj = JsonSerializer.Deserialize<List<List<object>>>(json);

The class should have at least one property. Your json doesnt' have any properties at all, so only array can be used in this case. It can be converted to the class, for example like this

public class MyClass
{
   public List<object> Details {get;set;}
}
var jsonParsed = JArray.Parse(json);

List<MyClass> objects = jsonParsed.Select(p => p)
.Select(x => new MyClass { Details=x.ToObject<List<object>>() }).ToList();

now if you want more details you have to convert array to your class, Each element of array should get name, for example

public class Details
{
public Id {get; set;}
public bool IsActive {get;set}
...and so on
}

after this you can use this class already

public class MyClass
{
   public Details Details {get;set;}
}
  • Related