Get Required Property from Complex json string in C# with Newtonsoft

Introduction

In this blog, I will tell you the trick to convert JSON string property to C# Object using Newtonsoft.

Illustration

Suppose the JSON string is like…

{
  "Success": true,
  "ComplexObject": {
    "NormalProp": "Test",
    "ListOfAnotherClass": [{
      "JustAStringProp": "We"
    }, {
      "JustAStringProp": "Got"
    }, {
      "JustAStringProp": "Values"
    }]
  }
}

Now, if I get this string from some method and try to process, I would need to convert this to C# objects. The structure would look something like below. To easily get the class structure, you can use any online tool like json2csharp.

class Result
{
	public bool Success { get; set; }
	public ComplextJsonClass ComplexObject { get; set; }
}

class ComplextJsonClass
{
	public string NormalProp { get; set; }
	public List<AnotherClass> ListOfAnotherClass { get; set; }
}

class AnotherClass
{
	public string JustAStringProp { get; set; }
}

What We Need?

So, for processing, I would like to get the “ComplexObject” property of this JSON string converted as “ComplextJsonClass” C# object. As the JSON string is complex with other property like “Success“, we only need the property “ComplexObject“, yet converted to a C# object.

How To Do That?

We will use Newtonsoft. You can easily find that from Nuget and add that as a reference to your project.

Solution

First of all, I will tell you how to generate this type of JSON.

var complex = new ComplextJsonClass
{
	NormalProp = "Test",
	ListOfAnotherClass = new List<AnotherClass>
	{
		new AnotherClass{JustAStringProp = "We"},
		new AnotherClass{JustAStringProp = "Got"},
		new AnotherClass{JustAStringProp = "Values"},
	}
};

var moreComplex = new Result {Success = true, ComplexObject = complex};
var jsonString = JsonConvert.SerializeObject(moreComplex);

This “jsonString” comes as I mentioned in the beginning of the post. Now, we need to convert this JSON to a C# Object. For this, we need to use JsonConvert.DeserializeObject Method (String).

var jsonObject = JsonConvert.DeserializeObject(jsonString);

But, unfortunately, this alone won’t help us more to access the properties. We have to convert to a JObject, by which we can easily access the properties from the JSON.

var jsonObject = (JObject)JsonConvert.DeserializeObject(jsonString);

After this, it is just a matter of getting the property by using jsonObject["ComplexObject"].

var jsonObject = (JObject)JsonConvert.DeserializeObject(jsonString);
if (jsonObject["ComplexObject"] == null) return;

ComplextJsonClass complexObject = JsonConvert.DeserializeObject<ComplextJsonClass>
            (jsonObject["ComplexObject"].ToString());
Convert to C# Object from Newtonsoft JObject

Convert to C# Object from Newtonsoft JObject

Full Code

var complex = new ComplextJsonClass
{
	NormalProp = "Test",
	ListOfAnotherClass = new List<AnotherClass>
	{
		new AnotherClass{JustAStringProp = "We"},
		new AnotherClass{JustAStringProp = "Got"},
		new AnotherClass{JustAStringProp = "Values"},
	}
};

// Get the jsonString from complex object.
var moreComplex = new Result {Success = true, ComplexObject = complex};
var jsonString = JsonConvert.SerializeObject(moreComplex);

//Now let's deserialize and convert to our required ComplextJsonClass object.
var jsonObject = (JObject)JsonConvert.DeserializeObject(jsonString);
if (jsonObject["ComplexObject"] == null) return;

ComplextJsonClass complexObject = JsonConvert.DeserializeObject<ComplextJsonClass>
            (jsonObject["ComplexObject"].ToString());

Feedback

Let me know if this trick helped you in any way in your projects or assignments. Re-post or share, if you liked it.

Thanks a lot for reading. Marry Christmas and Happy New Year. 🙂

Why ASP.NET is ASP.NET?

Funda

ASP :- Active Server Pages .NET :- .NET FrameworkASP.NET
According to Wiki

ASP.NET is a server-side Web application framework designed for Web development to produce dynamic Web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft’s Active Server Pages (ASP) technology.

So, simply, ASP.NET engine alters the aspx file (ASP.NET file extension is aspx), which contains some elements like TextBox, Label etc. and converts them to HTML control at the run time, before sending it to the user (browser).

Research

ASP.NET was a successor to ASP. At initial stage of the release, ASP.NET was called ASP+.
Refer – ASP.NET (ASP+).

ASP.NET (originally called ASP+) is the next generation of Microsoft’s Active
Server Page
(ASP), a feature of their Internet Information Server (IIS).
Both ASP and ASP.NET allow a Web site builder to dynamically build Web pages on the fly by
inserting queries to a relational database in the Web page.

Inference

So, the idea was much transparent. ASP.NET was developed by introducing major enhancement to ASP. With the announcement of .NET Framework, ASP+ was renamed to ASP.NET, just to make it compatible with new .NET Nomenclature.

Do you have any other views?

Please share your thoughts. Don’t forget to Like and Share the Blog.

Why .NET is .NET !!!

Background

This was indeed asked by one interviewer to me, when I was in Final Year of my Engineering.

Funda

Microsoft.Net
The full Form of .NET is Network Enabled Technologies.
.Net is a Framework, which can be used in the development of any kind of application like…

  • Desktop Application
  • Web Application
  • Mobile Application

Research

So, let’s have a research on our main Topic.
According to Wikipedia

Microsoft started development of .NET Framework in the late 1990s, originally under the name of Next Generation Windows Services (NGWS). By late 2000 the first beta versions of .NET 1.0 were released.

According to my research, it seems to be a Marketing and Branding Decision taken by Microsoft at that time.

When John Gage joined Sun Microsystems, he is credited with creating the phrase “The Network is the Computer”. So, if we think that .NET refers to Network, then it would not be wrong, as it was considered to be Microsoft‘s reaction to that statement. However, Microsoft was one step ahead to define “that statement” in a very “Productive Way”, which was going to create a “Software Development Revolution”.

Another aspect of the research reveals that Microsoft thought .NET could lessen the .COM mania.

Technical Aspect

Let’s give a thought to the Technical Aspect of the Nomenclature.
With .NET, we can create Web Services. The Framework makes life easier to write and consume Web Services. In this way, it has enhanced the Internet in terms of what it was doing and signifies the term Network in .NET. Anyone can access the Service from any platform and consume data to do operations. That was the main theme.

What I think?

Really does not matter, right?
Anyway, the Nomenclature is believed to be a gimmick, still I can’t ignore the Technical Aspect.

What you think?

It matters a lot. So, put your thoughts on this topic. Let’s discuss and find out the mystery.