JSON (JavaScript Object Notation) is a lightweight data-structure format for data interchange, JSON could be read by human and it is easy for machine to generate and pars it.
When should we use JSON?
Assume you want create a service that retrieve data from DataBase and send them to the client. What will happen if your client use different type of machines and platform same as Smart phone, PHP website, ASP.net Website, different type of operating system. There are two solutions:
- Create different services for different machines and platforms (is it economic?)
- Create a service that produces a global output that is readable by different machines and platforms.
I am sure you are agree with me that the second solution is more logical.
In my opinion WebAPI could be a good choice for our scenario because, it can produce JSON output that is readable by all machines and platform and also it uses HTTP to transfer data that, also this protocol is well known for all.
Today I am just going to show you how you can create a simple JSON data structure but, In the future I will show you how create a WebAPI and call it by JQuery.
This is simple JSON sample, I declare an employee array that can carry different employees’ data.
{ "employees": [ { "firstName":"Mehran" , "lastName":"JN" }, { "firstName":"Arman" , "lastName":"Nas" }, { "firstName":"Farzam" , "lastName":"ABS" } ] }
I am going to do it in a JavaScript code:
<!DOCTYPE html> <html> <body> <h2>JSON Object Creation in JavaScript</h2> <p> Name: <span id="Name"></span><br> Family: <span id="Family"></span><br> </p> <script> var JSONObject = { "Name":"Mehran", "Family":"Jan"}; document.getElementById("Name").innerHTML=JSONObject.Name document.getElementById("Family").innerHTML=JSONObject.Family </script> </body> </html>
NewtonSoft.Json is a library used for serialize and deserialize json. The ASP.NET Web API has a dependency in this library.
Good easy explanation. Maybe here are only general information about JSON but for beginners it is a good start point to learn this notation 🙂
Reblogged this on K Opoku Asare.