I have a JSON string like below
{"data":[{"report_header":{"c1":{"name":"COLUMN1","type":"java.lang.String"},"c2":{"name":"COLUMN2","type":"java.lang.String"}},"report_row":[{"c1":"2019","c2":"TEST123"},{"c1":"2019","c2":"TEST345"},{"c1":"2020","c2":"TEST567"}]}],"message":["OK"],"status":200}
I have used json2csharp and created classes.
public class C1
{
public string name { get; set; }
public string type { get; set; }
}
public class C2
{
public string name { get; set; }
public string type { get; set; }
}
public class ReportHeader
{
public C1 c1 { get; set; }
public C2 c2 { get; set; }
}
public class ReportRow
{
public string c1 { get; set; }
public string c2 { get; set; }
}
public class Datum
{
public ReportHeader report_header { get; set; }
public List<ReportRow> report_row { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
public List<string> message { get; set; }
public int status { get; set; }
}
And reading values using below code
var jss = new JavaScriptSerializer();
var ListExample = jss.Deserialize<RootObject>(result);
IList<Datum> listData = ListExample.data;
IList<ReportRow> reportRows = listData[0].report_row;
foreach (ReportRow row in reportRows)
{
Console.WriteLine(row.c1.ToString());
Console.WriteLine(row.c2.ToString());
}
My question is how I can modify above code to make it more generic so that in can handle more columns. Like what if I have more columns like c3,c4 in Jason. How can I iterate through them without re-writing Report Row class for all such use cases.
Json.Linqfor that. Your json sample seems incorrect