1

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

Hasitha Shan
  • 2,900
  • 6
  • 42
  • 83

2 Answers2

2

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.

Community
  • 1
  • 1
Dominik Kirschenhofer
  • 1,205
  • 1
  • 13
  • 27
  • thank you for the reply. :) i will check on this and be back :) – Hasitha Shan 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 Shan Aug 29 '12 at 10:56
  • wow that makes sense..ill do what you have said and get back :) thanks a bunch :) – Hasitha Shan 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 http://www.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 Shan Aug 29 '12 at 14:55
  • okey, i solved it and added my solution to the question itself :) .. please let me know if there is a better way of doing it..many thanks for your advice and help :) much appreciated :) – Hasitha Shan Aug 29 '12 at 15:08
  • Do you realy need this "corrData" stuff? I think you should be able to do "arr_from_json[0]" in your first edit. (Have not tried it yet) – Dominik Kirschenhofer Aug 30 '12 at 08:56
  • Just created a little sample. "arr_from_json[0].Ip" is working. Normaly it helps to use Firebug (Firefox extension), DeveloperTool (integrated in IE7+) or the Chrome DeveloperTools to debug such scripts to see what state you have got! – Dominik Kirschenhofer Aug 30 '12 at 09:03
  • okey..than k you very much !!!! i will try that out also :) :) thank you very much for your effort :) i just tried a sneaky way of doing it..thank you very much for pointing it that to me :).. – Hasitha Shan Aug 30 '12 at 09:20
2

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);
        }
    }
}
Sam Shiles
  • 10,529
  • 9
  • 60
  • 72
  • 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 Shan 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: http://stackoverflow.com/questions/227624/asp-net-mvc-controller-actions-that-return-json-or-partial-html – Sam Shiles Aug 29 '12 at 11:03
  • I made an edit to the code..can you please help me on this last bit...i tried searching online like http://stackoverflow.com/questions/4828207/how-to-use-a-json-file-in-javascript but couldn't get any value out :( – Hasitha Shan Aug 29 '12 at 14:26