Saturday, July 10, 2021

Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net

Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net

In this article I will explain with an example, how to use the Newtonsoft.Json.JsonConvert library for Serializing and Deserializing JSON using C# and VB.Net.

The JSON string will be first downloaded from an API using WebClient class and then will be deserialized to JSON object using the Deserialize function of the JSON.Net library.

Finally, the JSON object will be again serialized to JSON string using the Serialize function of the JSON.Net library in ASP.Net using C# and VB.Net.

Download JSON.Net library

The JSON.Net library is available for download from the following URL.

Download JSON.Net

The JSON string returned from the API

The following JSON string is returned from the Snippets Test API.

[

   {

      "CustomerId":1,

      "Name":"John Hammond",

      "Country":"United States"

   },

   {

      "CustomerId":2,

      "Name":"Mudassar Khan",

      "Country":"India"

   },

   {

      "CustomerId":3,

      "Name":"Suzanne Mathews",

      "Country":"France"

   },

   {

      "CustomerId":4,

      "Name":"Robert Schidner",

      "Country":"Russia"

   }

]

Namespaces

You will need to import the following namespaces.

C#

using System.Net;

using Newtonsoft.Json;

 

VB.Net

Imports System.Net

Imports Newtonsoft.Json

 

Property Class

The following property class will be used for deserializing the JSON string.

C#

public class Customer

{

    public int CustomerId { get; set; }

    public string Name { get; set; }

    public string Country { get; set; }

}

 

VB.Net

Public Class Customer

    Public Property CustomerId As Integer

    Public Property Name As String

    Public Property Country As String

End Class

Serializing and Deserializing JSON in C# and VB.Net

Inside the Page Load event, first the JSON string is downloaded from an API using DownloadString method of the WebClient class.

Then first, the JSON string is deserialized to JSON object using the Deserialize function of the JSON.Net library.

Finally, the JSON object will be again serialized to JSON string using the Serialize function of the JSON.Net library.

C#

protected void Page_Load(object sender, EventArgs e)

{

    if (!this.IsPostBack)

    {

        //Fetch the JSON string from URL.

        ServicePointManager.Expect100Continue = true;

        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

        string json = (new WebClient()).DownloadString("https://baladotnetblog.blogspot.com/snippets/test/master/Customers.json");

 

        //Deserialize the JSON string.

        List<Customer> customers = JsonConvert.DeserializeObject<List<Customer>>(json);

 

 

        //Serialize the JSON string.

        json = JsonConvert.SerializeObject(customers);

    }

}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    If Not Me.IsPostBack Then

        'Fetch the JSON string from URL.

        ServicePointManager.Expect100Continue = True

        ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)

        Dim json As String = (New WebClient).DownloadString("https://baladotnetblog.blogspot.com/snippets/test/master/Customers.json")

 

        'Deserialize the JSON string.

        Dim customers As List(Of Customer) = JsonConvert.DeserializeObject(Of List(Of Customer))(json)

 

        'Serialize the JSON string.

        json = JsonConvert.SerializeObject(customers)

    End If

End Sub

Screenshots

Deserializing to JSON object

Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net
Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net

Serializing to JSON string

Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net
Newtonsoft.Json.JsonConvert: Serializing and Deserializing JSON in C# and VB.Net


No comments:

Post a Comment

DotNet Latest Technologies

  I'm not sure exactly what you are looking for or how "recent" the technologies that you might wan to look into are, but I...