Newtonsoft Json Deserialize C# Example

Ecco Suprastyo
4 min readJun 19, 2019

--

You can read see FULL SECTION this article in this http://camellabs.com/newtonsoft-json-deserialize-csharp-example/ or Visit my site at camellabs.com.

This article about Newtonsoft Json Deserialize C# Example. The Newtonsoft.Json namespace provides classes that are used to implement the core services of the framework. Provides methods for converting between .NET types and JSON types.

Let’s following this tutorial in below to how to use newtonsoft.json c#.

  1. Create database in mysql with name “test” and create table with name “user”, like the below.

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.

3. Then will appear the window New Project like the look below

4. Write down the name of the project that will be created on a 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.

5. Create a new windows form like the below

6. Create a new class “modulePost” for request json parse from rest api client and write the following program listing :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO;
namespace newtonsoft_json
{
public class modulePost
{
public static string urlServer = "http://localhost/json/ws.php";

public void setURL(string url)
{
urlServer = url;
}
public string getURL()
{
return urlServer;
}
public string Post(string data)
{
string DataFromPHP = null;
try
{
string url = getURL();
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.Timeout = 1900000;
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
DataFromPHP = _Answer.ReadToEnd();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return DataFromPHP;
}
}

7. Next step, Back to windows form and view code to write the following program listing:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
namespace newtonsoft_json
{
public partial class Form1 : Form
{
modulePost _modul = new modulePost();
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string firstname,lastname,address;
string app;
string module;
string action;
string entity;
string data;
string respon;
string json;
try
{
app = "user";
module = "user";
action = "loginUser";
entity = "&username=" + txtUsername.Text + "&password=" + txtPassword.Text;
data = "&app=" + app + "&module=" + module + "&action=" + action + "" + entity;
respon = _modul.Post(data);
json = respon.TrimStart('[');
json = json.TrimEnd(']');
JObject o = JObject.Parse(json);
JArray jsonarray = (JArray)o["data"];
for (int i = 0; i < jsonarray.Count; i++)
{
JObject aItem = (JObject)jsonarray[i];
firstname = (string)aItem["firstname"];
lastname = (string)aItem["lastname"];
address = (string)aItem["address"];
MessageBox.Show("Data Found My Name is " + firstname + lastname + " and I Live at " + address);
}
}
catch
{
}
}
}
}

8. After you write down the program listings, press the F5 key to run the program and if you successfull connect your database the result is :

We have explained how to make a simple program with newtonsoft json post example c# using visual studio 2010 and mysql, for those of you who want to download the source code of the program also can. Hopefully this discussion is helpful to you.

You can see Newtonsoft Json Deserialize C# Example from Github project in Here.

Thank you for reading this article about Newtonsoft Json Deserialize C# Example, I hope this article is useful for you. Visit My Github about .Net Csharp in Here

--

--