Search code examples
c#jsonlistjson.netdeserialization

JSON.NET - Deserialize not working correctly


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\":\"abc@gmail.com\",\"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": "abc@gmail.com",
            "Pass": "12gfgh3a"
        }
    ]
}

Thanks.


Solution

  • 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