0

I have converted a JSON to datatable. But don't know how to display it sub array part.

json is :

 {
"School": "xxxx",
"Location": "yyyyy",
"TotalStaffs": "50",
"StudentsCount": [
 {
  "ClassA": "80",
  "ClassB": "60"
}
]
}

 DataTable dt2 = JsonConvert.DeserializeObject<DataTable>("[" + json + "]");

Now the output is :

School  Location  TotalStaffs  StudentsCount
 xxxx    yyyy        50  

     

The output should be :

School  Location  TotalStaffs   ClassA   ClassB
 xxxx    yyyy        50           80      60

NB : StudentsCount array is dynamic. There may be only one class sometime. Or may be more than one class. And the resulted table will always contain only one row, as shown in the sample.

Ken White
  • 123,280
  • 14
  • 225
  • 444
DTechy
  • 5
  • 4
  • There's probably a way to use something like newtonsofts `JObject.Parse(schema).SelectTokens("$..*")` to transform the input. Maybe based on https://stackoverflow.com/a/57900473/4139809 ? – Jeremy Lakeman May 12 '22 at 04:10

1 Answers1

1

Real output is correct. Your output breaks normal forms. But if you need your output view anyway, you must make it by yourself. The easiest example:

DataTable dt2 = JsonConvert.DeserializeObject();
string output = $"{dt2}\t{string.Join("\t", dt2.StudentsCount)}";