簡體   English   中英

將C#arraylist傳遞給Javascript

[英]pass C# arraylist to Javascript

我在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創建圖表。 請幫助我使用此方法將我的值傳遞給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中使用easiers方式,如您發布的鏈接中所述。 表示序列化列表並將其存儲在隱藏字段中。 當你需要在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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM