C# C中Highcharts数据对象的JSON结果#
我试图获取自定义类的列表,并在MVC/C中将其转换为JSON。我使用了json.encode和javascriptserializer,但都没有用。json数据显示为&.quot;而不是单引号或双引号。以下是我相信的相关代码。这些数据将用于highcharts应用程序 我的车型C# C中Highcharts数据对象的JSON结果#,c#,json,asp.net-mvc,highcharts,javascriptserializer,C#,Json,Asp.net Mvc,Highcharts,Javascriptserializer,我试图获取自定义类的列表,并在MVC/C中将其转换为JSON。我使用了json.encode和javascriptserializer,但都没有用。json数据显示为&.quot;而不是单引号或双引号。以下是我相信的相关代码。这些数据将用于highcharts应用程序 我的车型 public class DataPoint { public double? data { get; set; } //CTDI value public DateTime? DateOfScan {
public class DataPoint
{
public double? data { get; set; } //CTDI value
public DateTime? DateOfScan { get; set; }
public string Name { get; set; }
public string Nmmber{ get; set; }
public int PersonDatabaseID { get; set; }
public string EquipmentName{ get; set; }
}
public class PlotData : List<DataPoint>
{
}
public class PlotListModel : List<PlotData>
{
}
公共类数据点
{
公共双精度?数据{get;set;}//CTDI值
公共日期时间?日期扫描{get;set;}
公共字符串名称{get;set;}
公共字符串Nmmber{get;set;}
public int PersonDatabaseID{get;set;}
公共字符串设备名称{get;set;}
}
公共类PlotData:列表
{
}
公共类PlotListModel:列表
{
}
我的观点
foreach (PlotData pd in (PlotListModel) ViewBag.MyData)
{
JavaScriptSerializer js = new JavaScriptSerializer();
var jsData1 = js.Serialize(pd);
//Or use JSON.Encode
var jsData2 = Json.Encode(pd);
string divID = "container" + i;
<div id='@divID'>
<script type='text/javascript'>
$(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: '@divID',
type: 'colummn'
},
title: {text: '@divID'},
tooltip: {
formatter: function () {
return 'Extra data: <b>' + this.point.Scanner + '</b>';
}
},
series: [{
name: 'Foo',
data: ['@jsData1']
}]
});
});
</script>
</div>
i += 1;
}
When I look at the page source I see the data fields and values surrounded by the encoded quote value - "
foreach(PlotListModel)ViewBag.MyData中的PlotData-pd)
{
JavaScriptSerializer js=新的JavaScriptSerializer();
var jsData1=js.Serialize(pd);
//或者使用JSON.Encode
var jsData2=Json.Encode(pd);
字符串divID=“container”+i;
$(函数(){
var图表=新的Highcharts.图表({
图表:{
renderTo:“@divID”,
类型:“Column”
},
标题:{text:'@divID'},
工具提示:{
格式化程序:函数(){
返回“额外数据:”+this.point.Scanner+”;
}
},
系列:[{
名称:“Foo”,
数据:['@jsData1']
}]
});
});
i+=1;
}
当我查看页面源时,我看到数据字段和值被编码的引号值包围—
您应该创建一个webapi控制器,它应该包含一个类似这样的方法。我假设您在这个示例中使用的是webapi 2.0
[Route("api/plotlist")]
[HttpGet]
public PlotListModel GetPlotList() {
//Here's where you would instantiate and populate your model
PlotListModel plotList;
/*
* Once your model is instantiated and populated then you can
* just return it and the serialization will be done for you
*/
return plotList;
}
一旦有了它,就可以使用ajax从客户端代码调用该方法
function retreivePlotList() {
$.getJSON("http://<base url>/api/plotlist", function(data) {
//do something with data
});
}
函数retreivePlotList(){
$.getJSON(“http:///api/plotlist“,函数(数据){
//处理数据
});
}
以下是web api类
public class PlotListController : ApiController
{
[Route("api/plotlist")]
[HttpGet]
public IEnumerable<CTDIDataPoint> GetPlotList()
{....
你绝对应该使用Web API将数据序列化为JSON格式,并通过ajax调用从服务器请求数据。谢谢,Thomas。关于这方面的好教程有什么建议吗?除了谷歌搜索之外,不知道从哪里开始。太棒了,Casey。谢谢。我今晚会尝试一下。所以我有一个问题。方法不是called。我已经设置了一个停止,但它从未被命中。你能将url放入浏览器并调用该方法吗?不,我不能。我有localhost/api/plotList并得到404错误。你是否正确配置了属性路由?使用本教程进行双重检查。同时确保该方法位于ApiController中。控制器应继承f这是一个答案,还是你想澄清你的问题。
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}