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