您好在下面的代码中我尝试使用C#动态创建json并将json对象传递给客户端。我无法按照下面的静态json获得精确的json格式。您可以帮我解决这个问题。我的目标是使用C#获得json格式。
精确的Json格式:
{
"chart": {
"caption": "Quarterly Sales vs. Profit % in Last Year",
"subcaption": "Product-wise Break-up - Harry's SuperMart",
"xAxisName": "Quarter",
"pYAxisName": "Sales",
"sYAxisName": "Profit %",
"numberPrefix": "$",
"numbersuffix": "M",
"sNumberSuffix": "%",
"sYAxisMaxValue": "25",
"paletteColors": "#5598c3,#2785c3,#31cc77,#1aaf5d,#f45b00",
"baseFontColor": "#333333",
"baseFont": "Helvetica Neue,Arial",
"captionFontSize": "14",
"subcaptionFontSize": "14",
"subcaptionFontBold": "0",
"showBorder": "0",
"bgColor": "#ffffff",
"showShadow": "0",
"canvasBgColor": "#ffffff",
"canvasBorderAlpha": "0",
"divlineAlpha": "100",
"divlineColor": "#999999",
"divlineThickness": "1",
"divLineDashed": "1",
"divLineDashLen": "1",
"usePlotGradientColor": "0",
"showplotborder": "0",
"valueFontColor": "#ffffff",
"placeValuesInside": "1",
"showXAxisLine": "1",
"xAxisLineThickness": "1",
"xAxisLineColor": "#999999",
"showAlternateHGridColor": "0",
"legendBgAlpha": "0",
"legendBorderAlpha": "0",
"legendShadow": "0",
"legendItemFontSize": "10",
"legendItemFontColor": "#666666"
},
"categories": [
{
"category": [
{
"label": "Q1"
}
]
}
],
"dataset": [
{
"dataset": [
{
"seriesname": "Processed Food",
"data": [
{
"value": "30"
}
]
},
{
"seriesname": "Un-Processed Food",
"data": [
{
"value": "21"
}
]
}
]
},
{
"dataset": [
{
"seriesname": "Electronics",
"data": [
{
"value": "27"
}
]
},
{
"seriesname": "Apparels",
"data": [
{
"value": "17"
}
]
}
]
}
],
"lineset": [
{
"seriesname": "Profit %",
"showValues": "0",
"data": [
{
"value": "14"
}
]
}
]
}
C#:
object[,] arrData = new object[2, 3];
arrData[0, 0] = "Product A";
arrData[0, 1] = 567500;
arrData[0, 2] = 567500;
StringBuilder jsonData = new StringBuilder();
StringBuilder categories = new StringBuilder();
StringBuilder currentYear = new StringBuilder();
StringBuilder lineset = new StringBuilder();
jsonData.Append("{" +
//Initialize the chart object with the chart-level attributes..
"'chart': {"+
"'caption': 'Sales by Product'," +
"'numberPrefix': '$',"+
"'formatNumberScale': '1'," +
"'placeValuesInside': '1'," +
"'decimals': '0'" +
"},");
categories.Append("'categories': [" +
"{" +
"'category': [");
currentYear.Append("{" +
"'seriesname': 'Current Year'," +
"'data': [");
lineset.Append("'lineset': [" +
"{"
);
lineset.Append("{" +
"'seriesname': 'Current Year'," +
"'seriesname': 'Current Year'," +
"'data': [");
for (int i = 0; i < arrData.GetLength(0); i++)
{
if (i > 0)
{
categories.Append(",");
currentYear.Append(",");
}
categories.AppendFormat("{{" +
"'label': '{0}'" +
"}}", arrData[i, 0]);
currentYear.AppendFormat("{{" +
"'value': '{0}'" +
"}}", arrData[i, 1]);
lineset.AppendFormat("{{" +
// data level attributes
"'value': '{0}'" +
"}}", arrData[i, 2]);
}
categories.Append("]" +
"}" +
"],");
currentYear.Append("]" +
"}");
jsonData.Append(categories.ToString());
jsonData.Append("'dataset': [{");
jsonData.Append("'dataset': [");
jsonData.Append(currentYear.ToString());
jsonData.Append("]" +
"}");
jsonData.Append(lineset.ToString());
jsonData.Append("}" +
"]");
return jsonData.ToString();
答案 0 :(得分:3)
使用我的回答here从JSON创建一个C#类。然后创建一个类的实例并用这样的数据填充它:
var data = new WhateverYourClass();
// fill the properties
然后使用NewtonSoft NuGet包并执行此操作将您的类实例转换为JSON(序列化):
string json = JsonConvert.SerializeObject(data);