I need to have a json with this format (when data is null, just retrieve the time field):
var chartData = [
{
"time": "0",
"value": -0.307
},
{
"time": "1",
"value": -0.168
},
{
"time": "2"
},
{
"time": "3",
"value": -0.027
}
]
I have created two classes:
- dataV1 (time)
- dataV2 (time, value -> should be double)
Code:
public class dataV1
{
public string time { get; set; }
public dataV1(string Ptime)
{
this.time = Ptime;
}
public dataV1() { }
}
public class dataV2
{
public string time { get; set; }
public double value { get; set; }
public dataV2(string Ptime, double Pvalue)
{
this.time = Ptime;
this.value = Pvalue;
}
public dataV2() { }
}
Then in the C# sql:
if (sqlReader["value"] != DBNull.Value)
How can I combine both classes and use dataV1
when value is null and dataV2
when we have a not null value?
And retrieve a Json result
return Json(new
{
chartData,
}, JsonRequestBehavior.AllowGet);