【问题标题】:How to dynamically make a JSON object from code behind in c#如何从 C# 中的代码动态生成 JSON 对象
【发布时间】:2016-09-13 01:20:37
【问题描述】:

我想使用 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 新手,无法找到解决方案的确切方向。

【问题讨论】:

  • 创建一个 WebAPI 并包含 odate 服务协议将解决你的问题,请参阅下面的链接asp.net/web-api/overview/odata-support-in-aspnet-web-apiodata.org/blog/…
  • 这应该有多动态?在每个页面请求上生成?甚至在页面请求之间,使用 AJAX?
  • 你已经看过Json.NET了吗?
  • 既然你在谈论“代码背后”,你在使用WebForms吗?
  • @Marcel 在每个页面请求上生成,不,我没有检查过 Json,NET,是的,使用网络表单

标签: javascript c# asp.net json webforms


【解决方案1】:

是的,我们可以选择通过使用 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 的

【讨论】:

    【解决方案2】:

    一个详细的答案,可能会有所帮助

    创建类

    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
                })
     });
    

    谢谢

    【讨论】:

    • 如果您已经获得 JsonObject,请尝试 JordanW 提示,即使用 Newtonsoft.json 包的 JsonConvert.SerializeObject(myObj)
    • 这正是我所需要的。
    【解决方案3】:

    查看Newtonsoft json nuget 包。

    您可以调用一个方法将对象序列化为 Json

    例如。 JsonConvert.SerializeObject(myObj);

    【讨论】:

      猜你喜欢
      • 2022-08-24
      • 2016-04-30
      • 2020-06-08
      • 2023-03-09
      • 2011-01-19
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多