JSON
Tutorial
JSON or JavaScript Object Notation,
is a text-based open standard designed for human-readable data interchange. It
is derived from the JavaScript scripting language for representing simple data
structures and associative arrays, called objects. Despite its relationship to
JavaScript, it is language-independent, with parsers available for many
languages. The JSON format is often used for serializing and transmitting
structured data over a network connection. It is used primarily to transmit
data between a server and web application, serving as an alternative to XML.
Example:
How to send JSON data to Web method i.e.
‘GetData’
1) create New website in visual studio
2) Write Below code In java script as function
function SendData() {
var
jsonObjects = {
"items": [
{
"compId": "1","formId": "531"
},
{
"compId": "5","formId": "77"
},
{
"compId": "8","formId": "89"
}
]
};
$.ajax({
url: "Default.aspx/GetData",
data:
JSON.stringify(jsonObjects),
dataType: "json",
type: "POST",
contentType: "application/json;
charset=utf-8",
dataFilter:
function(data) { return
data; },
success: function(data) {
alert(data.d);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
}
3) Now add new Button to call Method and send data to
serverside
<asp:Button ID="Button4"
runat="server"
Text="Send JSON
Data in Object(s)" OnClientClick="SendData();return false;" />
4) Import
this libraries
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Security;
5) Use
this method to accept Json data and save it to RDBMS
[WebMethod]
public static
string GetData(object
items)
{
//SINGLE OBJECT SAVE DATA
List<object>
lstItems = new
JavaScriptSerializer().ConvertToType<List<object>>(items);
//now we can Acces lstItems values
return "Send Data
successfully." ;
}