简体   繁体   中英

C# Json combine two different objects

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);

You can have your dataV2 class (which i would advice to change its name to something more meaningful) have a double? nullable field instead of a double . That way, you won't have to duplicate your object for cases where there is "value" field in the JSON:

public class SomeData
{
    public string Time { get; set; }
    public double? Value { get; set; }

    public SomeData(string time, double? value)
    {
        this.time = time;   
        this.value = value;   
    }

    public SomeData() { }
}

And then deserializing it:

SomeData data = JsonConvert.DeserializeObject<SomeData>(json, 
                            new JsonSerializerSettings 
                            { NullValueHandling = NullValueHandling.Ignore });

You could inherit dataV1 by dataV2 ... then you can put them in a List<dataV1> :

public class ChartDataFactory //whatever... or directly in the controller though i don't recommend it
{
    public static IEnumerable<dataV1> GetChartData() //parameters ommited
    {
        List<dataV1> result = new List<dataV1>();

        //initialze connection/command/reader

        while (sqlReader.Read())
        {
            if (sqlReader["value"] != DBNull.Value) 
            {
                result.Add(new dataV1((string)sqlReader["time"]));
            }
            else
            {
                result.Add(new dataV2((string)sqlReader["time"],(double)sqlReader["value"]));
            }
        }
        // tear down connection
        return result;
    }
}


public class dataV1
{
    public string time { get; set; }

    public dataV1(string Ptime)
    {
        this.time = Ptime;
    }

    public dataV1() { }
}

public class dataV2 : dataV1
{
    public double value { get; set; }

    public dataV2(string Ptime, double Pvalue):base(Ptime)
    {
        this.value = Pvalue;
    }

    public dataV2() { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM