Introduction
JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON is a syntax to store and exchange the data. JSON is a text, written with JavaScript object notation.
Exchanging data
JSON.stringify()- Send
JSON.parse() - Receive
Data Format
{ "name":"Sakthi", "Designation":"Software Engineer", "Salary":10000 };
ASP page (or) HTML
- <form id="form2" runat="server">
- <div>
- <script>
- debugger;
- var obj, parameter, xmlhttp, _Obj, x, result = "";
- obj = {
- "Designation": "Software",
- "salary": 10000
- };
- dbParam = JSON.stringify(obj);
- xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = function() {
- if (this.readyState == 4 && this.status == 200) {
- _Obj = JSON.parse(this.responseText);
- result += "<table border='1'>"
- for (x in _Obj) {
- result += "<tr><td>" + _Obj[x].val + "</td></tr>";
- }
- result += "</table>"
- document.getElementById("demo").innerHTML = result;
- }
- };
- xmlhttp.open("POST", "./ReadTable.asmx/getdata", true);
- xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- xmlhttp.send("x=" + parameter);
- </script>
- <p id="demo"></p>
- </div>
- </form>
NOTE
Webservice Name ReadTable.asmx
Method Name getdata
ReadTable.cs
- [WebMethod]
- public void getdata(string x) {
- JavaScriptSerializer j = new JavaScriptSerializer();
- string json = new StreamReader(Context.Request.InputStream).ReadToEnd();
- Dictionary < string, string > sData = j.Deserialize < Dictionary < string, string >> (x);
- string Designation = sData["Designation"].ToString();
- String Salary = sData["Salary"].ToString();
- List < UserList > c = new List < UserList > ();
- SqlConnection con = new SqlConnection("");
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from TableName where Designation=@Designation and Salary=@Salary", con);
- cmd.Parameters.AddWithValue("@Designation", Designation);
- cmd.Parameters.AddWithValue("@Salary", Salary);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- for (int i = 0; i < dt.Rows.Count; i++) {
- UserList _userlist = new UserList();
- _userlist.val = dt.Rows[i][0].ToString();...c.Add(_userList);
- }
- JavaScriptSerializer j2 = new JavaScriptSerializer();
- Context.Response.Write(j2.Serialize(c));
- }
USERLIST CLASS FILE
- class UserList {
- public string val {
- get;
- set;
- }
- public string val2 {
- get;
- set;
- }
- public string val3 {
- get;
- set;
- }...
- }