我想在C#中创建这个数组,并通过Json将它发送到javascript。 我怎么能用C#写这个?
这是Javascript中的数组;
var aa = [{
name: 'D1',
data: [
[Date.UTC(2010, 0, 12, 12, 10, 10), 1],
[Date.UTC(2010, 0, 13, 18, 01, 04), 1],
[Date.UTC(2010, 0, 14, 14, 11, 44), 0],
[Date.UTC(2010, 0, 15, 14, 11, 44), 1],
[Date.UTC(2010, 0, 16, 14, 18, 44), 1],
[Date.UTC(2010, 0, 17, 15, 11, 44), 0],
[Date.UTC(2010, 0, 18, 16, 15, 44), 1]
]
}, {
name: 'D2',
data: [
[Date.UTC(2010, 0, 12), 1],
[Date.UTC(2010, 0, 13), 0],
[Date.UTC(2010, 0, 14), 0],
[Date.UTC(2010, 0, 15), 0],
[Date.UTC(2010, 0, 16), 1],
[Date.UTC(2010, 0, 16), 0],
[Date.UTC(2010, 0, 17), 0],
[Date.UTC(2010, 0, 18), 0]
]
}];
我尝试在c#中创建相同的数组:
public class chData
{
public DateTime Date { get; set; }
public int value { get; set; }
}
public class finallist
{
public string name { get; set;}
public List<chData> data { get; set;}
}
List<chData> D1data= new List<chData>
{
new chData {Date = new DateTime(2014, 1, 1, 00,00,01) , value = 2},
new chData {Date = new DateTime(2014, 1, 1, 00,00,05), value = 1},
new chData {Date = new DateTime(2014, 1, 1, 00,00,10), value = 1},
new chData {Date = new DateTime(2014, 1, 1, 00,00,15), value = 0},
new chData {Date = new DateTime(2014, 1, 1, 00,00,20), value = 1},
new chData {Date = new DateTime(2014, 1, 1, 00,00,25), value = 3},
new chData {Date = new DateTime(2014, 1, 1, 00,00,30), value = 2},
new chData {Date = new DateTime(2014, 1, 1, 00,00,35), value = 1},
new chData {Date = new DateTime(2014, 1, 1, 00,00,40), value = 1},
new chData {Date = new DateTime(2014, 1, 1, 00,00,45), value = 3},
new chData {Date = new DateTime(2014, 1, 1, 00,00,50), value = 1},
new chData {Date = new DateTime(2014, 1, 1, 00,01,15), value = 2}
};
List<finallist> Finaldata= new List<finallist>();
Finaldata.Add(new finallist { name = "D1", data = D1data});
return JsonConvert.SerializeObject(Finaldata);
我在C#中使用数组的结构有一些问题,它不起作用! 我确信我在C#代码中做错了。
这是我的javascript代码
var aa =[];
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/getData",
data: "{}",
dataType: "json",
success: function (data) {
aa = eval((data.d).replace(/\/Date\((.*?)\)\//gi, "new Date($1)"));
},
error: function (result) {
alert('#Err');
}
});
});
答案 0 :(得分:3)
要获取您想要的JSON结构,您需要更改代码,以便data
属性的每个元素都是一个对象数组:
public class finallist {
public string name { get; set; }
public List<object[]> data { get; set; }
}
var D1data = new List<object[]>
{
new object[] { new DateTime(2014, 1, 1, 00,00,05), 1},
new object[] { new DateTime(2014, 1, 1, 00,00,10), 1},
new object[] { new DateTime(2014, 1, 1, 00,00,15), 0},
new object[] { new DateTime(2014, 1, 1, 00,00,20), 1},
new object[] { new DateTime(2014, 1, 1, 00,00,25), 3},
new object[] { new DateTime(2014, 1, 1, 00,00,30), 2},
new object[] { new DateTime(2014, 1, 1, 00,00,35), 1},
new object[] { new DateTime(2014, 1, 1, 00,00,40), 1},
new object[] { new DateTime(2014, 1, 1, 00,00,45), 3},
new object[] { new DateTime(2014, 1, 1, 00,00,50), 1},
new object[] { new DateTime(2014, 1, 1, 00,01,15), 2}
};
List<finallist> Finaldata = new List<finallist>();
Finaldata.Add(new finallist { name = "D1", data = D1data });
return JsonConvert.SerializeObject(Finaldata, Formatting.Indented);
答案 1 :(得分:2)
finallist.data应该是一个数组数组:
var convertable = Finaldata.Select(f => new
{
f.name,
data = f.data.Select(d => new object[] { d.Date, d.value })
});
return JsonConvert.SerializeObject(convertable);
结果将等同于你的json:
[{"name":"D1","data":[
["2014-01-01T00:00:01",2],
["2014-01-01T00:00:05",1], ...