c# - 将 C# arraylist 传递给 Javascript

标签 c# javascript object arraylist

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

这是我的 ConfidenceLvl 类,

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 创建图表。请帮助我使用这个this method将我的值传递给 javascript,但它们都是一个字符串。我想分别获取这些值并在 javascript 中使用它们。请帮我。非常感谢。

编辑: 感谢 Dominik Kirschenhofer,在成功解析到 javascript 后,我​​终于解决了这个问题。我可以知道如何操作这些数据吗?喜欢一张一张地记录吗??我试过了,但没有成功,

    <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 以便我可以像在数组中一样通过整数 ID 调用它。 :) 谢谢大家的帮助 :) 如果有更好的方法..请告诉我 :)

最佳答案

如果您在要在 js 中使用的代码中有一个列表或固定值数组,则更简单的方法如您发布的链接中所述。意味着序列化列表并将其存储在隐藏字段中。当你需要在 js 中使用它时,只需反序列化并使用它。

序列化见: http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx

反序列化: deserialize from json to javascript object

那么你需要做什么: 1)使用通用列表。 2) 根据需要填写列表。 3) 将列表转换为数组 => 使用 linq 非常简单:myList.ToArray() 4)将数组序列化为json字符串。见第一个链接。 5)在你的页面上放置一个隐藏字段并将json字符串设置为它的值(代码隐藏) 6)每当js中需要用到这个数组的时候,只要反序列化隐藏字段的值就可以使用了。请参阅第二个链接。

关于c# - 将 C# arraylist 传递给 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12175342/

相关文章:

javascript - 如何在 JavaScript localStorage 中存储(字典)对象?

jQuery - 如何在另一个对象中存储和访问一个对象

c# - 在 C# 中获取 JSON POST 数据

c# - 从字符串中删除空格

c# - C#中事件的主要目的

电话号码的 Javascript 验证(数字、空格、点、连字符)

c# - MEF 运行时插件更新问题

javascript - 使用 Lodash 将数组转换为嵌套对象,这可能吗?

javascript - 如何在 LimeJS javascript for html5 中处理键盘事件

javascript - 如何访问和处理嵌套对象、数组或 JSON?