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
Full Code
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);
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.
CodeProject