将动态 json 添加到静态 json

Add dynamic json to static json

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

以下代码片段有助于创建图形:

                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 似乎无法解析 - 谁能帮帮我 :)

试试这个。

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

points: <%= getJson() %>

这会输出 return 结果类型,例如:

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

因此,要确定输出,请逐个从外部到内部 method/object。

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

""

第 2 步:序列化 List<Object>(基本上是一个 JavaScript 数组)

"[]"

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

"[{}]"

第 4 步:先序列化 属性 :

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

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