简体   繁体   English

如何在C#中为此类型创建json对象?

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

I am working on highcharts and sending data in ajax from c#. 我正在研究图表,并从c#发送ajax数据。 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, 并填写此属性的列表,并使用javascriptserializer对其进行序列化并存储此json字符串,并返回至我使用JSON.parse进行解析的ajax成功,

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 ,简单,简单,免费...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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