将C#数组列表传递给Javascript
本文关键字:Javascript 列表 数组 | 更新日期: 2025-02-19 09:51:26
我在C#中有一个arrayList,我在其中存储ip和isp的数据,并从mysql表中进行如下计数
ArrayList conRc = new ArrayList();
while (readIp.Read())
{
string ipVal = readIp.GetString(0);
string ispVal = readIp.GetString(1);
int conLvlVal = readIp.GetInt32(2);
conRc.Add(new ConfidenceLvl(ipVal,ispVal,conLvlVal));
}
这是我的信心等级,
public class ConfidenceLvl
{
private string ip;
private string isp;
private int conLvl;
public string Ip
{
get { return ip; }
set { ip = value; }
}
public string Isp
{
get { return isp; }
set { isp = value; }
}
public int ConLvl
{
get { return conLvl; }
set { conLvl = value; }
}
public ConfidenceLvl(string ipVal, string ispVal, int conLvlVal) {
this.conLvl = conLvlVal;
this.ip = ipVal;
this.isp = ispVal;
}
}
我想把它传递给javascript,这样我就可以利用这些值通过jqPlot创建一个图表。请帮助我使用这个方法将我的值传递给javascript,但它都是一个字符串。我希望将这些值分开,并在javascript中使用它们。请帮帮我。非常感谢。
编辑:多亏了Dominik Kirschenhofer,在成功地将解析为javascript之后,我终于完成了这项工作。我可以知道如何处理这些数据吗?比如1比1拿到唱片??我试过了,但没有成功,
<asp:HiddenField ID="correlate" value= "" runat="server" />
<script language="javascript" type="text/javascript">
var jsonString = document.getElementById('correlate').value;
var arr_from_json = JSON.parse(jsonString);
</script>
json字符串如下,
[{"Ip":"0","Count":"10"},{"Ip":"1","Count":"11"},{"Ip":"2","Count":"12"},{"Ip":"3","Count":"13"},{"Ip":"4","Count":"14"},{"Ip":"5","Count":"15"},{"Ip":"6","Count":"16"},{"Ip":"7","Count":"17"},{"Ip":"8","Count":"18"},{"Ip":"9","Count":"19"}]
我可以知道如何处理这些记录吗:)
编辑:已解决
<asp:HiddenField ID="correlate" value= "" runat="server" />
<script language="javascript" type="text/javascript">
var jsonString = document.getElementById('correlate').value;
jsonString = "{'"corrData'":" + jsonString + "}";
var arr_from_json = JSON.parse(jsonString);
alert(Number(arr_from_json.corrData[2].Ip)+1);
</script>
我添加了corrData,这样我就可以用数组中的Integer Id来调用它。:)谢谢你们的帮助,伙计们:)如果有更好的方法的话。。请告诉我:)
如果你在代码后面有一个固定值的列表或数组,你想在js中使用,更简单的方法是在你发布的链接中解释的。意味着序列化列表并将其存储在隐藏字段中。当你需要在js中使用它时,只需反序列化并使用它。
序列化请参阅:http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx
反序列化:从json反序列化为javascript对象
那么你该怎么办呢1) 使用通用列表。2) 根据需要填写列表。3) 使用linq:myList.ToArray()很容易将列表转换为数组=>4) 将数组序列化为json字符串。请参阅第一个链接。5) 在页面上放置一个隐藏字段,并将json字符串设置为其值(代码隐藏)6) 每当你需要在js中使用这个数组时,只需反序列化隐藏字段的值并使用它。请参阅第二个链接。
如果您使用的是ASP.net MVC,那么您可以执行以下操作。我已经重写了您的代码,使其使用泛型列表,而不是ArrayList。ArrayList不是强类型的,通常被认为是贬值的,因为泛型与.net 2.0 一起出现
请注意,以下代码仅适用于POST请求,除非您将其修改为包含JsonRequestBehavior.AllowGet
public class ConfidenceLvl
{
public string IpVal { get; set; }
public string IspVal { get; set; }
public string ConLvlVal { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index()
{
IList<ConfidenceLvl> levels = new List<ConfidenceLvl>();
levels.Add(new ConfidenceLvl { IpVal = "129.123.123.123", ConLvlVal = "1", IspVal = "AOL" });
levels.Add(new ConfidenceLvl { IpVal = "129.123.0.0", ConLvlVal = "3", IspVal = "CompuServe" });
// Controlled json
return Json(levels.Select(lv => new { title = lv.IspVal, ip = lv.IpVal }));
// As it comes json
//return Json(levels);
}
}
}