How do I turn a C object into a JSON string in NET

0 votes

These are my classes

class MyDate
{
    int year, month, day;
}

class Lad
{
    string firstName;
    string lastName;
    MyDate dateOfBirth;
}

And I want to turn a Lad into a JSON string like:

{
    "firstName":"Markoff",
    "lastName":"Chaney",
    "dateOfBirth":
    {
        "year":"1901",
        "month":"4",
        "day":"30"
    }
}

Other than manually developing a JSON string writer, is there any other option?

Jun 11, 2022 in C# by pranav
• 2,590 points
719 views

1 answer to this question.

0 votes

Newtonsoft.json can be used to accomplish this. NuGet is used to install Newtonsoft.json. Like this

using Newtonsoft.Json;

var jsonString = JsonConvert.SerializeObject(obj);
answered Jun 13, 2022 by krishna
• 2,820 points

Related Questions In C#

0 votes
1 answer

How do I specify the exit code of a console application in .NET

Simply put, you can return the exit ...READ MORE

answered Jun 11, 2022 in C# by pranav
• 2,590 points
2,460 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
642 views
0 votes
0 answers

How can I split a string with a string delimiter

Suppose this is my string My name is ...READ MORE

Jun 11, 2022 in C# by krishna
• 2,820 points
343 views
0 votes
0 answers

How do I determine the size of my array in C?

How do I figure out my array's ...READ MORE

Nov 16, 2022 in C# by Ashwini
• 5,430 points
336 views
0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,620 points
458 views
0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

The following is my classes class MyDate { ...READ MORE

Sep 26, 2022 in C# by krishna
• 2,820 points
455 views
0 votes
1 answer

SQLite.Net not being able to create text file in Win IoT Library!

That exception comes when access limited to ...READ MORE

answered Aug 3, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,014 views
0 votes
2 answers

Is there a .NET equivalent to Apache Hadoop?

Hadoop is a Java-based platform. So, to ...READ MORE

answered Jul 16, 2020 in Big Data Hadoop by Suhana
• 340 points
1,738 views
0 votes
1 answer

How to convert JSON text into objects using C#

To make a class out of a ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
561 views
0 votes
1 answer

How to send email in ASP.NET C#

You can try this using Hotmail like ...READ MORE

answered Jun 14, 2022 in C# by krishna
• 2,820 points
949 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP