简体   繁体   English

Newtonsoft.Json.ReferenceLoopHandling.忽略反序列化格式 .NET Core 3.1

[英]Newtonsoft.Json.ReferenceLoopHandling.Ignore deserialization format .NET Core 3.1

I'm trying to make an API using .NET Core 3.1 with C# programming language which utilizes Firebase real-time database from Google through FireSharp Nu-Get package. I'm trying to make an API using .NET Core 3.1 with C# programming language which utilizes Firebase real-time database from Google through FireSharp Nu-Get package. When I ran it, I got this exception: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.当我运行它时,我得到了这个异常: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. I read here that I can just install Microsoft.AspNetCore.Mvc.NewtonsoftJson package and ignore the reference loop handling, which works fine. System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.在这里读到我可以只安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson package 并忽略参考循环处理,这工作正常。 But the data format looks unreadable, very hard to deserialize, and way too long:但是数据格式看起来不可读,很难反序列化,而且太长了:

{
"stateMachine": {
    "<>1__state": 0,
    "<>t__builder": {},
    "bpjs": "12345678",
    "reservationTime": "24-11-2020",
    "<>4__this": {}
},
"context": {},
"moveNextAction": {
    "method": {
        "name": "MoveNext",
        "declaringType": ...
...
((lots of irrelevant things))
...

    }
},
"result": {
    "queueNumber": "1",
    "bpjs": "12345678",
    "name": "Addi",
    "poli": "Umum",
    "reservationTime": "24-11-2020",
    "status": "Queueing"
},
"id": 2,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false}

Before this, I already tried to do some mapping and not directly use my data model by following this video 's tutorial on doing DTOs, but it didn't work most likely because I don't use SQL Database.在此之前,我已经尝试按照此视频的 DTO 教程进行一些映射,而不是直接使用我的数据 model,但它很可能不起作用,因为我不使用 SQL 数据库。

Now I'm trying to use this API on a Flutter app.现在我正在尝试在 Flutter 应用程序上使用这个 API。 Is there anyway for me to maybe do some minor changes on my API code (anything that doesn't involve completely changing the database to SQL) to reformat my response?无论如何,我是否可以对我的 API 代码(不涉及将数据库完全更改为 SQL 的任何内容)进行一些小的更改以重新格式化我的响应? Or maybe a way to just partly deserialize my response, since I only need the "result" part and not anything else?或者也许只是部分反序列化我的响应的方法,因为我只需要“结果”部分而不需要其他任何东西?

Edit:编辑:

This is my main code to get the data:这是我获取数据的主要代码:

        public async Task<QueueData> GetQueueData(string bpjs, string reservationTime)
    {
        //set up connection
        IFirebaseConfig config = new FirebaseConfig
        {
            AuthSecret = myAuthSecret,
            BasePath = myBasePath
        };

        IFirebaseClient client = new FireSharp.FirebaseClient(config);

        //checkqueuenumber
        string queueCounter = await QueueDbConnection.CheckQueueNumber(reservationTime, client);

        //getresult
        QueueData result = await QueueDbConnection.GetResult(bpjs, reservationTime, queueCounter, client);
        return result;
    }

this is my controller to call the function above:这是我的 controller 调用上面的 function:

        [HttpGet("{bpjs}/{reservationTime}")]
    public ActionResult<QueueData> GetQueueData(string bpjs, string reservationTime)
    {
        var queueData = _repository.GetQueueData(bpjs, reservationTime);
        return Ok(queueData);
    }

This is my model for the QueueData, which is the type I'm trying to return as response:这是我的 QueueData 的 model,这是我试图作为响应返回的类型:

    public class QueueData
{
    [Required]
    public string QueueNumber { get; set; }
    [Required]
    public string BPJS { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Poli { get; set; }
    [Required]
    public string ReservationTime { get; set; }
    [Required]
    public string Status { get; set; }
}

this is another model class called control to monitor the number of people queueing each day:这是另一个 model class 调用控件来监控每天排队的人数:

    public class QueueCounter
{
    [Required]
    public string q { get; set; }
}

So, I solved this.所以,我解决了这个问题。 I went back to here and apparently someone said the error I got in the first place was because not all async functions had been waited or handled properly.我回到这里,显然有人说我首先遇到的错误是因为并非所有异步函数都已被等待或正确处理。 So I went through my codes once again and finally found the problem in my controller.所以我再次检查了我的代码,最后在我的 controller 中发现了问题。 After I made my controller async then changed the return type into Task<ActionResult>, everything went perfectly fine!在我使我的 controller 异步然后将返回类型更改为 Task<ActionResult> 之后,一切都很好!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Newtonsoft Json.NET ReferenceLoopHandling和JavascriptDateTimeConverter - Newtonsoft Json.NET ReferenceLoopHandling and JavascriptDateTimeConverter 在Newtonsoft.json中,ReferenceLoopHandling.Ignore究竟做了什么? - What does ReferenceLoopHandling.Ignore in Newtonsoft.json exactly do? System.Version 的序列化和反序列化使用 Newtonsoft,dotnet core 2.2 vs dot net core 3.1 - Serialization and Deserialization of System.Version using Newtonsoft, dotnet core 2.2 vs dot net core 3.1 Newtonsoft json.net反序列化NullReferenceException - Newtonsoft json.net deserialization NullReferenceException Newtonsoft JSON.NET反序列化错误 - Newtonsoft JSON.NET Deserialization error .NET CORE 3.1 - [HttpPost] - JSON 值无法转换为 Newtonsoft.Json.Linq.JToken - .NET CORE 3.1 - [HttpPost] - The JSON value could not be converted to Newtonsoft.Json.Linq.JToken JSON 在没有 Newtonsoft 的 ASP.Net Core 中反序列化多态和复杂对象 - JSON deserialization of polymorphic and complex objects in ASP.Net Core without Newtonsoft 设置为ReferenceLoopHandling.Ignore时,json.net不会忽略循环引用 - json.net doesn't ignore circular references when set to ReferenceLoopHandling.Ignore 在 ASP.NET Core 3.1 中使用 Newtonsoft.JSON 的自定义 JsonConverter 属性不适用于 Json.Serialize() - Custom JsonConverter attributes not working with Json.Serialize() using Newtonsoft.JSON in ASP.NET Core 3.1 如何使用Newtonsoft.Json反序列化时忽略类型 - How to ignore type while Deserialization using Newtonsoft.Json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM