'How to convert json with subarray to data table
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.
Solution 1:[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)}";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AnatolyBelanov |
