Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an arrayList in C# where i store data of ip's and isp's and count as follows from a mysql table,

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

This is my ConfidenceLvl Class,

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;
    }
}

I wan to pass this to javascript so that i can make use of these value to create a chart through jqPlot. Please help on this one I used this method to pass my values to javascript but it all goes as one string. I wan to take these values separately and work with them in javascript. Please help me. Thank you very much.

EDIT: Thanks to Dominik Kirschenhofer, I finally wound up with this after successfully parsing the to javascript. May i know how to manipulate these data? like getting records 1 by 1?? i tried to but none worked,

    <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>

The json string is as follows,

    [{"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"}] 

May i know how can i work with these records :)

EDIT: SOLVED IT

    <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>

I added corrData so that i can call it by an Integer Id like in an array. :) thanks for your help guys :) if there is better way..please let me know :)

share|improve this question
    
see stackoverflow.com/questions/7846333/… may give you an idea... –  Amol Kolekar Aug 29 '12 at 10:10
    
Are you using webforms or mvc? –  Sam Shiles Aug 29 '12 at 10:13
    
yea am using webforms :) –  Hasitha Aug 29 '12 at 10:36

2 Answers 2

up vote 2 down vote accepted

If you are having a list or an array of fixed values in code behind which you want to use in js the easiers way is as explained in the link you have posted. Means serialize the list and store it in an hidden field. When you need to use it in js just deserialize and use it.

Serialization see: http://blogs.microsoft.co.il/blogs/pini_dayan/archive/2009/03/12/convert-objects-to-json-in-c-using-javascriptserializer.aspx

Deserialization: deserialize from json to javascript object

So what do you have to do: 1) Use a generic list. 2) Fill the list as needed. 3) Convert the list to an array => pretty easy using linq: myList.ToArray() 4) Serialize the array to a json string. See 1st link. 5) Place a hidden field on your page and set the json string as its value (code behind) 6) Whenever you need to use this array in js, just deserialize the value of the hidden field and use it. See 2nd link.

share|improve this answer
    
thank you for the reply. :) i will check on this and be back :) –  Hasitha Aug 29 '12 at 10:34
    
hey..i tried researching this method..can you clarify..may be some links.. :) because i am new to the web development and i am kind of lost here. –  Hasitha Aug 29 '12 at 10:56
    
I have modified my post! –  Dominik Kirschenhofer Aug 29 '12 at 11:42
    
wow that makes sense..ill do what you have said and get back :) thanks a bunch :) –  Hasitha Aug 29 '12 at 13:13
    
I made an edit to the code..can you please help me on this last bit...i tried searching online like json.org/js.html but couldn't get any value out :( ..i want to retrieve records whenever i want from it :) thank you very much –  Hasitha Aug 29 '12 at 14:55

If you're using ASP.net MVC then you can do the following. I have rewritten your code to use a generic list, instead of an ArrayList, The ArrayList is not strongly typed and is generally considered depreciated since generics came along with .net 2.0

Please note, that the following code will only work with a POST request, unless you modified it to include the 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);
        }
    }
}
share|improve this answer
    
thank you very much for ur reply and time.i will check this method :) –  Hasitha Aug 29 '12 at 10:34
    
I am using webforms, is there a difference between those two..sorry if this is a dumb question.but i am new to web development in asp.net –  Hasitha Aug 29 '12 at 10:47
    
Yes, this method will not work for webforms. See the suggested link for a way of doing it with webforms: stackoverflow.com/questions/227624/… –  Sam Shiles Aug 29 '12 at 11:03
    
okey..i will study this post. thank you very much.. :) –  Hasitha Aug 29 '12 at 11:12
    
I made an edit to the code..can you please help me on this last bit...i tried searching online like stackoverflow.com/questions/4828207/… but couldn't get any value out :( –  Hasitha Aug 29 '12 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.