简体   繁体   中英

How to make json object for this type in c#?

I am working on highcharts and sending data in ajax from c#. it was working fine now i am stuck with some different type of chart data here

previously when i have to send data to chart i simply create properties of series,categories etc

public class SeriesQuarter2
{
    public string id { get; set; }
    public string name { get; set; }
    public List<Data2> data { get; set; }
}

and fill the list of this property and serialize it using javascriptserializer and store this json string and return to ajax success where i parse using JSON.parse,

so the chart series which i want to make looks like this

series: [
    {
        id: '1',
        name: 'Tokyo',
        data: [-49.9, 71.5]
    },
    {
        id: '2',
        name: 'New York',
        data: [83.6, -78.8]
    }
]

Now this is fine, but now i have to send object for this type of series

series: [
    {
        type: 'column',
        name: 'Tokyo',
        data: [-49.9, 71.5]
    },
    {
        type: 'column',
         name: 'New York',
        data: [83.6, -78.8]
    },
    {
        type: 'scatter',
        showInLegend: false,
        data: [
            {
                name:'Mydata',
                y: 200,
                mydata : 4,
                color:'red',
                marker: {
                    symbol: 'url(http://www.highcharts.com/demo/gfx/snow.png)'
                }
            }, 
            {
                name:'Mydata2',
                y: 200,
                mydata : -4,
                marker: {
                    symbol: 'url(http://www.highcharts.com/demo/gfx/sun.png)'
                }   
            }
        ]
    }
]

How will i make this type of object which has different properties?

Ok so here's an idea how it could look like:

class RootObject
{
    public List<SeriesItem> series { get; set; } = new List<SeriesItem>();
}

class SeriesItem
{
    public string type { get; set; }
    public string name { get; set; }
    public bool? showInLegend  { get; set; }
    public List<object> data  { get; set; } = new List<object>();
}

class OtherData
{
    public int y { get; set; }
    public int mydata { get; set; }
    public string name { get; set; }
    public string color { get; set; }
    public Marker marker { get; set; }
}

class Marker
{
    public string symbol { get; set; }
}

Usage:

var item1 = new SeriesItem();
item1.type = "column";
item1.name = "Tokyo";
item1.data = new List<object>();
item1.data.AddRange(numbersData);
item1.data.Add(-49.9);
item1.data.Add(71.5);

var otherData1 = new OtherData();
otherData1.name = "MyData";
//...fill other fields

var item2 = new SeriesItem();
item2.type = "scatter";
item2.data.Add(otherData1);

var jsonObject = new RootObject();
jsonObject.series.Add(item1);
jsonObject.series.Add(item2);

//convert jsonObject to json

我的建议是使用任何第三方序列化程序 ,例如Newtonsoft json.net ,简单,简单,免费...

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