1

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.

4
  • If it doesn't even execute the line you need, probably response.IsSuccessful is false. Commented Dec 20, 2022 at 6:45
  • Nope. I checked by putting the breakpoint it comes inside the if block Commented Dec 20, 2022 at 6:46
  • Why then do you say that line isn't executed? If it comes into the if block, it comes to the deserializing line. Commented Dec 20, 2022 at 6:50
  • Nope that's the strange thing after coming to that line it never comes to next statement. Commented Dec 20, 2022 at 7:29

2 Answers 2

4

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 the documents array.

using Newtonsoft.Json.Linq;

var root = JObject.Parse(response.Content);
var list1 = (root["documents"] as JArray).ToObject<List<object>>(); 

Alternative: Define the Root class and deserialize as Root object.

public class Root
{
    [JsonProperty("documents")]
    public List<object> Documents { get; set; }
}
var root = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response.Content);
var list1 = root.Documents; 

Updated

The syntax in the attached JSON is MongoDB BsonDocument. I would suggest using MongoDB.Bson library to deserialize the response.

public class Root
{
    [BsonElement("documents")]
    public List<Document> Documents { get; set; }   
}

public class Document
{
    public ObjectId Id { get; set; }
    public int Role { get; set; }
    public int UserId { get; set; }
    public string Email { get; set; }
    public string Pass { get; set; }
}
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;

Root root = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<Root>(response.Content);
var list1 = root.Documents; 

Demo @ .NET Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

I like the Linq one. Could you please tell me how to get the object member and value by using Foreach. I am s new to this please help me.
Now it coming like this- {{ "_id": { "$oid": "639f5cb70d319f7bb78a6e0d" }, "Role": { "$numberInt": "2" }, "UserId": { "$numberInt": "2" }, "Email": "abcd", "Pass": "sdgsdgg" }} So how do i get to my object !
Hi, your JSON response for me is the MongoDB BsonDocument. Should deserialize with MongoDB.Bson. Check out my latest answer.
You are Great man. You have saved me finally. This is exactly what I wanted. Thanks a lot.
1

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,

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class Document
    {
        public Id _id { get; set; }
        public Role Role { get; set; }
        public UserId UserId { get; set; }
        public string Email { get; set; }
        public string Pass { get; set; }
    }

    public class Id
    {
        [JsonProperty("$oid")]
        public string oid { get; set; }
    }

    public class Role
    {
        [JsonProperty("$numberInt")]
        public string numberInt { get; set; }
    }

    public class Root
    {
        public List<Document> documents { get; set; }
    }

    public class UserId
    {
        [JsonProperty("$numberInt")]
        public string numberInt { get; set; }
    }

then deserialize the object like

Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(response.Content);

1 Comment

This might work one little problem is that for every classes I would have to generate separate classes like this. isn't it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.