I want to deserialize a response from RestResponse
in C# (MAUI).
request.AddBody(body);
RestResponse response = await client.PostAsync(request);
if (response.IsSuccessful)
{
var list1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(response.Content);
}
Here, inside the if
block the response I want in a list of objects but this line is not executing I mean it never comes to the end statement of the if
block. What to do? There are so many answers available for this but I tried so many of them, did not work for me. All are saying to do it like this:
Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(response.Content)
I initially put my class name List<MyClassName>
then thought about how would the deserializer know the members so replaced it with just object. No Luck!
Please have look at the response:
"{"documents":[{"_id":{"$oid":"639f5cb70d319f7bb78a6e0d"},"Role":{"$numberInt":"2"},"UserId":{"$numberInt":"2"},"Email":"abc","Pass":"dgdfgdfh"},{"_id":{"$oid":"639f5d3a0d319f7bb78a6e0e"},"Role":{"$numberInt":"2"},"UserId":{"$numberInt":"3"},"Email":"abcd","Pass":"dgdgdfh"},{"_id":{"$oid":"639f5d900d319f7bb78a6e10"},"Role":{"$numberInt":"2"},"UserId":{"$numberInt":"4"},"Email":"abcde","Pass":"dfgdfhdfhh"},{"_id":{"$oid":"639f5db80d319f7bb78a6e11"},"Role":{"$numberInt":"2"},"UserId":{"$numberInt":"5"},"Email":"abcdef","Pass":"dfhjlfdkjh"},{"_id":{"$oid":"639f5e170d319f7bb78a6e13"},"Role":{"$numberInt":"0"},"UserId":{"$numberInt":"1"},"Email":"fhdfhg","Pass":"ch"},{"_id":{"$oid":"639f79a373de576753175098"},"Email":"admin"},{"_id":{"$oid":"63a027f853fa7d5e3456d224"},"Role":{"$numberInt":"2"},"Email":"[email protected]","Pass":"12gfgh3a"}]}"
or you may look at this (same):
{
"documents":
[
{
"_id":
{
"$oid": "639f5cb70d319f7bb78a6e0d"
},
"Role":
{
"$numberInt": "2"
},
"UserId":
{
"$numberInt": "2"
},
"Email": "abc",
"Pass": "fhddfh"
},
{
"_id":
{
"$oid": "639f5d3a0d319f7bb78a6e0e"
},
"Role":
{
"$numberInt": "2"
},
"UserId":
{
"$numberInt": "3"
},
"Email": "abcd",
"Pass": "dfhfdh"
},
{
"_id":
{
"$oid": "639f5d900d319f7bb78a6e10"
},
"Role":
{
"$numberInt": "2"
},
"UserId":
{
"$numberInt": "4"
},
"Email": "shiva",
"Pass": "fgsdgsdg"
},
{
"_id":
{
"$oid": "639f5db80d319f7bb78a6e11"
},
"Role":
{
"$numberInt": "2"
},
"UserId":
{
"$numberInt": "5"
},
"Email": "abcdef",
"Pass": "dfgsdfgsdg"
},
{
"_id":
{
"$oid": "639f5e170d319f7bb78a6e13"
},
"Role":
{
"$numberInt": "0"
},
"UserId":
{
"$numberInt": "1"
},
"Email": "abcdefg",
"Pass": "dgsdgch"
},
{
"_id":
{
"$oid": "639f79a373de576753175098"
},
"Email": "admin"
},
{
"_id":
{
"$oid": "63a027f853fa7d5e3456d224"
},
"Role":
{
"$numberInt": "2"
},
"Email": "[email protected]",
"Pass": "12gfgh3a"
}
]
}
Thanks.
2
Answers
Your JSON content is an object with contains the
documents
property which is an array type.You should deserialize the
response.Content
as the root object and extract thedocuments
array.Alternative: Define the
Root
class and deserialize asRoot
object.Updated
The syntax in the attached JSON is MongoDB BsonDocument. I would suggest using MongoDB.Bson library to deserialize the response.
Demo @ .NET Fiddle
From your json input, you can have a respective model class. Use any online tools to convert to
c# class model
objects, i have used https://json2csharp.com/ and below is class model output,then deserialize the object like