【问题标题】:Add dynamic json to static json将动态 json 添加到静态 json
【发布时间】:2015-04-20 16:30:38
【问题描述】:

我在创建一些动态创建图表的代码时遇到了困难:

以下代码 sn-p 有助于创建图表:

                xGrid: false,
                legend: true,
                title: 'Meetings and Hours Used',
                points: [
                    [7, 26, 33, 74, 12, 49, 33, 33, 74, 12, 49, 33],
                    [32, 46, 75, 38, 62, 20, 52, 75, 38, 62, 20, 52]
                ],

我已经用这个替换了积分部分:

points: <%= getJson() %>

我后面的代码有功能:

    public string getJson()
    {
        var publicationTable = new List<object>{
        new { points = "[1,2,3,4,5,6,7,8]," }


    };
        return (new JavaScriptSerializer()).Serialize(publicationTable);
    }

javascript 似乎没有解析 - 谁能帮帮我:)

【问题讨论】:

  • 这是 MVC 项目吗?
  • points = "[1,2,3,4,5,6,7,8]," 中没有多余的逗号吗?

标签: javascript c# jquery json serialization


【解决方案1】:

试试这个。

public string getJson() {
    var publicationTable = new[] {
        new[] { 1, 2, 3, 4, 5 },
        new[] { 6, 7, 8, 9, 10 }
    };
    return (new JavaScriptSerializer()).Serialize(publicationTable);
}

【讨论】:

    【解决方案2】:
     List<List<int>> lstMainArr = new List<List<int>>();
            lstMainArr.Add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ToList<int>());
            lstMainArr.Add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.ToList<int>());
    
            Console.Write((new JavaScriptSerializer()).Serialize(lstMainArr));
    

    【讨论】:

      【解决方案3】:

      点数:

      这会输出任何返回结果类型,例如:

      points: 8      // getJson() returns int
      points: test   // getJson() returns a string
      

      如果你这样做了:

      points = "[1,2,3,4,5,6,7,8],";
      return points:
      

      结果是:

      points = [1,2,3,4,5,6,7,8],
      

      但是你有:

      var publicationTable = new List<object> {
        new { points = "[1,2,3,4,5,6,7,8]," }
      };
      
      return (new JavaScriptSerializer()).Serialize(publicationTable);
      

      因此,要确定输出,请逐步执行每个从外部到内部的方法/对象。

      第 1 步:序列化 - 创建一个空字符串。

      ""
      

      第 2 步:序列化 List&lt;Object&gt;(基本上是一个 JavaScript 数组)

      "[]"
      

      第 3 步:序列化第一个匿名对象:

      "[{}]"
      

      第 4 步:序列化第一个属性:

      "[{"points" : "[1,2,3,4,5,6,7,8],", }]"
      

      没有更多的属性,没有更多的对象,序列化完成。 This is not the JSON you were looking for.

      【讨论】:

        猜你喜欢
        • 2013-07-16
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        • 2021-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多