What is JavaScript in short?
JavaScript is a client-side scripting language designed mainly for adding interactivity to web pages and altering document content.
What is a JavaScript Object?
All these (a, b, and c) are JavaScript objects.
JavaScript Object Notation (JSON)
JSON is a lightweight data interchange format with the following characteristics.
- Human readable
- Easy to parse
- Lightweight
JSON Objects
The is no such thing as JSON objects. JSON is plain text that we use to transfer data.
JSON is not specific to JavaScript. We can parse a JSON string using any other programming language, like PHP, .Net, Pearl, and so on.
- d is a JSON string
- e is a JavaScript Object
- f is also a JavaScript Object, we just parse a JSON string into a JavaScript Object.
- d and e both look the same. So most developers feel that d is a JSON object.
I hope you all understand well.
var EmpUser = { "Id": "13", "Fname": "sai" };
var st = JSON.stringify(EmpUser);
debugger;
$.ajax({
type: "POST",
url: "http://localhost:35798/MyService.svc/MerchantPOST",
data: JSON.stringify(EmpUser),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert('success');
// Play with response returned in JSON format
},
error: function(result) {
alert("Error");
}
});
GET in JSON with WCF
[WebGet(UriTemplate = "GetAllMerchants", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
[OperationContract]
List<Merchant> GetAllMerchants();
public List<Merchant> GetAllMerchants()
{
// Returning List of merchants
List<Merchant> ob = new List<Merchant>();
ob.Add(new Merchant() { Fname = "sai", Id = 1 });
ob.Add(new Merchant() { Fname = "sukesh", Id = 2 });
ob.Add(new Merchant() { Fname = "pradeep", Id = 3 });
return ob;
}
POST in JSON with WCF
[WebInvoke(Method = "POST", UriTemplate = "MerchantPOST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
[OperationContract]
void AddMerchant(Merchant newEmp);
public void AddMerchant(Merchant newEmp)
{
//write your code to add merchant
}
GET data from the Normal Page method
var name = "sai";
var Address = "Azde Dombivali";
debugger;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "normalpagemethod.aspx/GetCustomer",
data: "{'username':'" + name + "','Address':'" + Address + "'}",
dataType: "json",
success: function (data) {
var obj = data.d;
$('#tblcustomer').append("<tr><td></td><td></td></tr>");
alert("Success");
},
error: function (result) {
alert("Error");
}
});
[WebMethod]
public static List<customer> GetCustomer(string username, string Address)
{
List<customer> objcust = new List<customer>();
objcust.Add(new customer() { Name = "sai", Address = "Azde" });
objcust.Add(new customer() { Name = "sukesh", Address = "khambalpada" });
return objcust;
}
If you want to learn JSON practically, then download the Zip file and check it.
In the Zip file, I have written examples of the use of GET, POST, PUT, and DELETE of JSON with a WCF service and normal page methods and JSON Serialization in C#.
Thanks for reading.