JSON.NET -反序列化无法正常工作

lh80um4z  于 2022-12-24  发布在  .NET
关注(0)|答案(2)|浏览(120)

我想在C#(MAUI)中反序列化来自RestResponse的响应。

request.AddBody(body);
RestResponse response = await client.PostAsync(request);
if (response.IsSuccessful)
{
    var list1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(response.Content);
}

这里,在if块中,我想要的响应在一个对象列表中,但是这一行没有执行,我的意思是它从来没有到达if块的结束语句。怎么办?有这么多的答案,但是我尝试了这么多,对我不起作用。所有人都说这样做:

Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(response.Content)

我最初把我的类名List<MyClassName>,然后想反序列化器如何知道成员,所以用just object替换了它。
请看下面的回答:

"{\"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\"}]}"

或者你可以看看这个(同样的):

{
    "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"
        }
    ]
}

谢谢。

fnx2tebb

fnx2tebb1#

您的JSON内容是一个包含documents属性(数组类型)的对象。
您应该将response.Content反序列化为根对象,并提取documents数组。

using Newtonsoft.Json.Linq;

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

备选方案:定义Root类并反序列化为Root对象。
x一个一个一个一个x一个一个二个x

    • 已更新**

所附JSON中的 * 语法 * 是MongoDB BsonDocument。我建议使用 * MongoDB. Bson * 库来反序列化响应。
一个三个三个一个
Demo @ .NET Fiddle

zour9fqk

zour9fqk2#

从你的json输入,你可以有一个各自的模型类。使用任何在线工具转换成c# class model对象,我已经使用了https://json2csharp.com/,下面是类模型输出,

// 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; }
    }

然后反序列化对象

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

相关问题