Difference Between JSON Object and JavaScript Object

What is JavaScript in short?

JavaScript is a client-side scripting language designed mainly for adding interactivity to web pages and alter the 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 characterstics:
  1. Human readable
  2. Easy to parse
  3. 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 JSON string
  • e is 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.

 

  1. var EmpUser = { "Id""13""Fname""sai"};  
  2.       var st = JSON.stringify(EmpUser);  
  3.       debugger;  
  4.       $.ajax({  
  5.           type: "POST",  
  6.            url: "http://localhost:35798/MyService.svc/MerchantPOST",  
  7.            data: JSON.stringify(EmpUser),  
  8.            contentType: "application/json; charset=utf-8",  
  9.            dataType: "json",  
  10.            success: function (data)  
  11.            {  
  12.                  alert('success');  
  13.                  // Play with response returned in JSON format  
  14.             },  
  15.             error: function (result)  
  16.             {   
  17.                  alert("Error");  
  18.             }  
  19.  });  
GET in JSON with WCF
  1. [WebGet(UriTemplate = "GetAllMerchants", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)][OperationContract]  
  2. List<Merchant> GetAllMerchants();  
  3.   
  4.   public List<Merchant> GetAllMerchants()  
  5.   {  
  6.      //returning List of merchants  
  7.      List<Merchant> ob = new List<Merchant>();  
  8.      ob.Add(new Merchant() { Fname = "sai", Id = 1 });  
  9.      ob.Add(new Merchant() { Fname = "sukesh", Id = 2 });  
  10.      ob.Add(new Merchant() { Fname = "pradeep", Id = 3 });  
  11.   
  12.      return ob;  
  13.   }  
POST in JSON with WCF
  1. [WebInvoke(Method = "POST", UriTemplate = "MerchantPOST",   
  2. ResponseFormat = WebMessageFormat.Json,  
  3. RequestFormat = WebMessageFormat.Json)]  
  4. [OperationContract]  
  5. void AddMerchant(Merchant newEmp);  
  6.     
  7. public void AddMerchant(Merchant newEmp)  
  8. {  
  9.    //write ur code to add merchant  
  10. }  
GET data from Normal Page method
  1. var name = "sai";  
  2. var Address = "Azde Dombivali";  
  3. debugger;  
  4.  $.ajax({  
  5.     type: "POST",  
  6.     contentType: "application/json; charset=utf-8",  
  7.     url: "normalpagemethod.aspx/GetCustomer",  
  8.     data: "{'username':'" + name + "','Address':'" + Address + "'}",  
  9.     dataType: "json",  
  10.     success: function (data)  
  11.     {  
  12.        var obj = data.d;  
  13.        $('#tblcustomer').append("<tr><td></td><td></td></tr>")  
  14.   
  15.        alert("Success");  
  16.   
  17.     },  
  18.     error: function (result)  
  19.     {  
  20.        alert("Error");  
  21.     }  
  22. });  
  23.   
  24. [WebMethod]  
  25. public static List<customer> GetCustomer(string username, string Address)  
  26. {  
  27.     List<customer> objcust = new List<customer>();  
  28.     objcust.Add(new customer() { Name = "sai", Address = "Azde" });  
  29.     objcust.Add(new customer() { Name = "sukesh", Address = "khambalpada" });  
  30.     return objcust;  
  31. }  
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.

Up Next
    Ebook Download
    View all
    Learn
    View all