[英]How to dynamically make a JSON object from code behind in c#
我想使用JSON对象制作流程图
HTML
var chart = null;
$(document).ready(function(){
chart = new FlowChart($);
var chartJSON = {
nodes: [
{ id: 'p1', type: 'simple-node', left: 120 ,top:2200 , content:'Process 1'},
{ id: 'p2', type: 'simple-node', left: 120,top: 2400,content:'Process 2' },
{ id: 'p3', type: 'simple-node', left: 120, top: 2600,content:'Process 3'}
],
connections: [
{ start: 'p1', end: 'p2' },
{ start: 'p2', end: 'p3' },
]
};
chart.importChart(chartJSON);
这样会在页面上创建流程图
但是我需要根据sql查询结果动态地从后面的代码中填充此json,我是javascript新手,无法找到解决方案的确切方向。
是的,我们可以选择使用javascript seralize和desearlize选项动态地传递给后面的代码。
例如:
result = JSON.stringify(p)// from client side it serialize json object
var a =new JavaScriptSerializer().Deserialize<class>(result) // server side seralize
注意使用system.web.serialization或newtonsoftjson dll的
详细的答案可能会有所帮助
创建类似的类
public class connections
{
public string start { get; set; }
public string end { get; set; }
}
public class chartItem
{
public string id { get; set; }
public string type { get; set; }
public int left { get; set; }
public int top { get; set; }
public string content { get; set; }
}
// holding both chartItems and nodes
public class ChartJson
{
public List<connections> connections { get; set; }
public List<chartItem> nodes { get; set; }
}
我正在使用WebApiController,也可以使用PageMethods
public class ChartController : ApiController
{
public ChartJson Get()
{
ChartJson chartJson = new ChartJson();
chartJson.nodes = getNodes(); //function to get data from database
chartJson.connections = getConnections(); //function to get data from database
return chartJson;
}
}
在.aspx页上
在.aspx页面上,使用下面的jQuery调用函数
$(function () {
$.getJSON("api/Chart/Get", function (result) {
console.log(result.connections); //Check results and bind with your chart object
console.log(result.nodes); //Check results and bind with your chart object
})
});
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.