Newtonsoft JSON Deserialize in C# with Example

Introduction 

 
This article is about Newtonsoft JSON deserializing with a C# example. The Newtonsoft.JSON namespace provides classes that are used to implement the core services of the framework. It provides methods for converting between .NET types and JSON types.
 
Let’s follow the tutorial below to learn how to use newtonsoft.json with C#.
 
Step 1
 
Create a database in MySQL with the name “test” and create a table with the name “user”, like shown below.
 
Newtonsoft Json Deserialize C# Example
 
Step 2
 
Create a new application project. In Visual Studio, on the menu click File> New > Project. For more details, see the following menu on the display.
 
Newtonsoft Json Deserialize C# Example
 
Step 3
 
Then the window 'New Project' will appear, which should look like the screenshot below:
 
Newtonsoft Json Deserialize C# Example
 
Step 4
 
Write down the name of the project that will be created in the field Name. Specify the directory storage project by accessing the field Location. Next, give the name of the solution in the Solution Name. Then click OK.
 
Newtonsoft Json Deserialize C# Example
 
Step 5
 
Create a new windows form like the one shown below:
 
Newtonsoft Json Deserialize C# Example
 
Step 6
 
Create a new class “modulePost” for requesting json parse from the rest api client. Write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Newtonsoft.Json.Linq;  
  6. using System.Net;  
  7. using System.IO;  
  8.   
  9. namespace newtonsoft_json  
  10. {  
  11.     public class modulePost  
  12.     {  
  13.         public static string urlServer = "http://localhost/json/ws.php";  
  14.          
  15.         public void setURL(string url)  
  16.         {  
  17.             urlServer = url;  
  18.         }  
  19.   
  20.         public string getURL()  
  21.         {  
  22.            return urlServer;  
  23.         }  
  24.   
  25.         public string Post(string data)  
  26.         {  
  27.             string DataFromPHP = null;  
  28.             try  
  29.             {  
  30.                 string url = getURL();  
  31.                 byte[] buffer = Encoding.ASCII.GetBytes(data);  
  32.                 HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);  
  33.                 WebReq.Timeout = 1900000;  
  34.                 WebReq.Method = "POST";  
  35.                 WebReq.ContentType = "application/x-www-form-urlencoded";  
  36.                 WebReq.ContentLength = buffer.Length;  
  37.                 Stream PostData = WebReq.GetRequestStream();  
  38.                 PostData.Write(buffer, 0, buffer.Length);  
  39.                 PostData.Close();  
  40.                 HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();  
  41.                 Stream Answer = WebResp.GetResponseStream();  
  42.                 StreamReader _Answer = new StreamReader(Answer);  
  43.                 DataFromPHP = _Answer.ReadToEnd();  
  44.             }  
  45.             catch (Exception ex)  
  46.             {  
  47.                 Console.WriteLine(ex.ToString());  
  48.             }  
  49.             return DataFromPHP;  
  50.         }  
  51.   

Step 7
 
Next, go back to the windows form and view the code in order to write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using Newtonsoft.Json.Linq;  
  10.   
  11. namespace newtonsoft_json  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         modulePost _modul = new modulePost();  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void btnLogin_Click(object sender, EventArgs e)  
  22.         {  
  23.             string firstname,lastname,address;  
  24.             string app;  
  25.             string module;  
  26.             string action;  
  27.             string entity;  
  28.             string data;  
  29.             string respon;  
  30.             string json;  
  31.             try  
  32.             {  
  33.                 app = "user";  
  34.                 module = "user";  
  35.                 action = "loginUser";  
  36.   
  37.                 entity = "&username=" + txtUsername.Text + "&password=" + txtPassword.Text;  
  38.                 data = "&app=" + app + "&module=" + module + "&action=" + action + "" + entity;  
  39.                 respon = _modul.Post(data);  
  40.   
  41.                 json = respon.TrimStart('[');  
  42.                 json = json.TrimEnd(']');  
  43.                 JObject o = JObject.Parse(json);  
  44.                 JArray jsonarray = (JArray)o["data"];  
  45.                 for (int i = 0; i < jsonarray.Count; i++)  
  46.                 {  
  47.                     JObject aItem = (JObject)jsonarray[i];  
  48.                     firstname = (string)aItem["firstname"];  
  49.                     lastname = (string)aItem["lastname"];  
  50.                     address = (string)aItem["address"];  
  51.   
  52.                     MessageBox.Show("Data Found My Name is " + firstname + lastname + " and I Live at " + address);  
  53.                 }  
  54.             }  
  55.             catch  
  56.             {  
  57.   
  58.             }  
  59.         }  
  60.     }  

Step 8
 
After you write down the program listings, press the F5 key to run the program. If you successfully connect your database, the result is:
 
Newtonsoft Json Deserialize C# Example
 
We have explained how to make a simple program with an example in C# using Visual Studio 2010 and MySQL. For those of you who want to download the source code of the program, you also can. Hopefully this discussion is helpful to you.
 
You can see the Deserialize C# Example from the Github project Here.
 
Thank you for reading this article, I hope it was useful for you. Visit My Github about .NET Csharp Here