DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • JSON-Based Serialized LOB Pattern
  • Adding Two Hours in DataWeave: Mule 4
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Trending

  • A System Cannot Protect What It Does Not Understand
  • Detecting Plan Regression in SQL Server Using Query Store
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  1. DZone
  2. Coding
  3. Languages
  4. How to Convert C# Object Into JSON String with JSON.NET

How to Convert C# Object Into JSON String with JSON.NET

By 
Jalpesh Vadgama user avatar
Jalpesh Vadgama
·
Apr. 14, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
193.3K Views

Join the DZone community and get the full member experience.

Join For Free

Before some time I have written a blog post – Converting a C# object into JSON string in that post one of reader, Thomas Levesque commented that mostly people are using JSON.NET a popular high performance JSON for creating for .NET Created by James Newton- King. I agree with him if we are using .NET Framework 4.0 or higher version for earlier version still JavaScriptSerializer is good. So in this post we are going to learn How we can convert C# object into JSON string with JSON.NET framework.

What is JSON.NET:
JSON.NET is a very high performance framework compared to other serializer for converting C# object into JSON string. It is created by James Newton-Kind. You can find more information about this framework from following link.

http://james.newtonking.com/json

How to convert C# object into JSON string with JSON.NET framework:
For this I am going to use old application that I have used in previous post. Following is a employee class with two properties first name and last name.

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I have created same object of “Employee” class as I have created in previous post like below.

Employee employee=new Employee
{FirstName = "Jalpesh",
LastName = "Vadgama"};

Now it’s time to add JSON.NET Nuget package. You install Nuget package via following command.

Nuget Pacake command for JSON.NET

I have installed like below.

How to install nuget package for JSON.NET from package manager console

Now we are done with adding NuGet package. Following is code I have written to convert C# object into JSON string.

string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(employee);
Console.WriteLine(jsonString);

Let's run application and following is a output as expected.

Convert C# object into JSON string with JSON.NET framework

That’s it. It’s very easy. Hope you like it. Stay tuned for more.

JSON Object (computer science) Strings Data Types Convert (command)

Published at DZone with permission of Jalpesh Vadgama. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JSON-Based Serialized LOB Pattern
  • Adding Two Hours in DataWeave: Mule 4
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook