动态创建带有 C# 代码的 javascript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5918302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:47:44  来源:igfitidea点击:

Dynamically create javascript array with c# code behind

c#javascriptasp.netserialization

提问by flavour404

I am updating an old classic ASP site to a new .net 3.5 version. The page has a custom list control which the client (my boss) wants to keep. This list control requires several arrays in order to work correctly. the array is a multi-dimensional list of publications. This is what it looks like:

我正在将旧的经典 ASP 站点更新为新的 .net 3.5 版本。该页面有一个客户(我的老板)想要保留的自定义列表控件。此列表控件需要多个数组才能正常工作。该数组是一个多维出版物列表。这是它的样子:

var publicationTable = [
    [31422,"Abilene Reporter News","Abilene","TX",false,"D",0],
    [313844,"Acadiana Weekly","Opelousas","LA",false,"W",1],
    [527825,"Action Advertiser","Fond du Lac","WI",false,"W",2]...n]

I want to generate this array server side and register it. I have looked at the msdnbut this is a little trivial. The conceptual issue is that the array is a mixture of string and ints and I am not sure how to recreate this, so how?

我想生成这个数组服务器端并注册它。我看过msdn但这有点微不足道。概念上的问题是数组是字符串和整数的混合体,我不确定如何重新创建它,那么如何呢?

回答by Edgar Villegas Alvarado

You should do this:

你应该做这个:

Code behind:

后面的代码:

using System.Web.Script.Serialization;
...
public string getJson(){
   var publicationTable = new List<object>{
      new []{ 31422,"Abilene Reporter News","Abilene","TX",false,"D",0},
      new []{ 313844,"Acadiana Weekly","Opelousas","LA",false,"W",1 },
      new []{ 527825,"Action Advertiser","Fond du Lac","WI",false,"W",2}
   };
   return (new JavaScriptSerializer()).Serialize(publicationTable);
}

Ase you see, to create an array of mixed types, we create an array of anonymous type with new []. You could also have done it with new object[].

如您所见,要创建混合类型的数组,我们使用new []. 你也可以用new object[].

Aspx file:

Aspx文件:

<script>
    var publicationTable = <%= getJson() %>;
</script>

Hope this helps. Cheers

希望这可以帮助。干杯

回答by spender

I think a List<List<object>>containing your items, passed through the JavaScriptSerializerwould do the trick. Given that this data probably comes from a more structured data type, you could probably do better than List<List<object>>, but the JavaScriptSerializeris probably what you're after.

我认为一个List<List<object>>包含你的物品,通过JavaScriptSerializer就可以了。鉴于此数据可能来自更结构化的数据类型,您可能会比 做得更好List<List<object>>,但这JavaScriptSerializer可能就是您所追求的。

回答by Bala R

Will this work?

这会奏效吗?

ArrayList list = new ArrayList{31422,"Abilene Reporter News","Abilene","TX",false,"D",0};

var str = string.Format("[{0}]", list.Cast<object>().Select(x => (x is string)?
          string.Format(@"""{0}""", x) : x.ToString())
          .Aggregate((x, y) => string.Format("{0}, {1}", x, y)));

it can be extended for a multi-dimensional array in a similar fashion.

它可以以类似的方式扩展到多维数组。