我试图以下列方式在c#中格式化JSON。可以说我有下表
Name col2 col3
Name1 1 2
Name2 4 5
Name3 7 8
我希望我的JSON输出像这样
[' Name1',' Name2',' Name3']
和
[' 1',' 4',' 7']
[' 2',' 5',' 8']
现在我有以下代码:
public string Test()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conect))
{
using (SqlCommand cmd = new SqlCommand("Select * from BASE.[dbo].[Orders_Date_to_Chart]() order by y desc", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new
System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object rows = new
List<Dictionary<string, object();
Dictionary<string, object row;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
}
}
现在我有结果:
[{&#34;名称&#34;:&#34;名1&#34;&#34; COL2&#34;:1,&#34; COL3&#34;:&#34; 2&#34 ;}, {&#34;名称&#34;:&#34;名称2&#34;&#34; COL2&#34:4,&#34; COL3&#34;:&#34; 5&#34;}, {&#34;名称&#34;:&#34; NAME3&#34;&#34; COL2&#34;:7,&#34; COL3&#34;:&#34; 8&#34;}] < / p>
答案 0 :(得分:0)
您可以使用JsonConvert.DeserializeObject(string)将其转换为您自己选择的自定义对象。一种方法可能是这样的:
public class RootObject
{
public List<ChildObject> ObjList { get; set; }
}
public class ChildObject
{
public string Name { get; set; }
public int col2 { get; set; }
public int col3 { get; set; }
}
在你的Test方法中这样做:
var rootObj = new RootObject();
rootObj.ObjList = JsonConvert.DeserializeObject<RootObject>(your string);
那会给你一个对象列表然后你可以根据属性做一个循环分配给新列表
答案 1 :(得分:0)
最后,我使用这种方法:
public string mies()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(dod.conect))
{
using (SqlCommand cmd = new SqlCommand("select miesiac from Sales.dbo.Month", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}
StringBuilder jsonString = new StringBuilder();
if (dt.Rows.Count > 0)
{
jsonString.Append("\"");
jsonString.Append("[");
for (int j = 0; j < dt.Columns.Count; j++)
{
for (int i = 0; i < dt.Rows.Count; i++)
if (i < dt.Rows.Count - 1)
{
jsonString.Append("'" + dt.Rows[i][j].ToString() + "'" + ",");
}
else if (i == dt.Rows.Count - 1)
{
jsonString.Append("'" + dt.Rows[i][j].ToString() + "'");
}
}
jsonString.Append("]");
jsonString.Append("\"");
}
return jsonString.ToString();
}
结果此字符串为:
"['2017-01','2017-02','2017-03','2017-04','2017-05','2017-06','2017-07','2017-08','2017-09','2017-10','2017-11','2017-12','2018-01','2018-02','2018-03','2018-04']"
和JavaScript:
var Score= document.getElementById("<%= HiddenFieldWunikTMUB.ClientID %>").value;
var Month= document.getElementById("<%= HiddenFieldMonth.ClientID %>").value;
Highcharts.chart('Wykresliniowy', {
chart: {
type: 'line'
},
title: {
text: 'Monthly Average Temperature'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: JSON.parse(Month)
},
yAxis: {
title: {
text: 'Temperature (°C)'
}
},
plotOptions: {
line: {
dataLabels: {
enabled: true
},
enableMouseTracking: false
}
},
series: [{
name: 'Berlin',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8, 5, 10, 56, 10]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8,5,10,56,10]
}]
});
}
我的图表有错误的xAxis: enter image description here
我需要这样:enter image description here
我做错了什么?