将C#arraylist传递给Javascript

时间:2012-08-29 09:59:11

标签: 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一样调用它。 :)感谢您的帮助:)如果有更好的方法..请让我知道:))

2 个答案:

答案 0 :(得分:2)

如果您要在js中使用一个列表或一组固定值,您可以在js中使用easiers方式,如您发布的链接中所述。表示序列化列表并将其存储在隐藏字段中。当你需要在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)将列表转换为数组=&gt;使用linq非常简单:myList.ToArray() 4)将数组序列化为json字符串。见第一个链接。 5)在页面上放置一个隐藏字段并将json字符串设置为其值(后面的代码) 6)每当你需要在js中使用这个数组时,只需反序列化隐藏字段的值并使用它。见第二个链接。

答案 1 :(得分:2)

如果您正在使用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);
        }
    }
}
相关问题