Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

25 Dec 2015 1  
In this blog, I will tell you the trick to convert JSON string property to C# Object using 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. Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here