ASP.NET中的JSON避难所不正常

发布于 2025-02-07 12:47:07 字数 1502 浏览 1 评论 0原文

我有一个API,该API返回以下响应为字符串

[
 {
    "id": "62a9f8f90346133662624bd3",
    "referenceID": "test1",
    "additionalInfoList": ["string"]
 },
 {
    "id": "62a9fba50346133662624bd4",
    "referenceID": "111",
    "additionalInfoList": ["string"]
 }
]

编辑:其中字符串的确切格式如下所示,与后挡板相比:

"[{\"id\":\"62a9f8f90346133662624bd3\",\"referenceID\":\"test1\",\"additionalInfoList\":[\"string\"]},{\"id\":\"62a9fba50346133662624bd4\",\"referenceID\":\"111\",\"additionalInfoList\":[\"string\"]}]"

以下类模型

public class IncidentModel
{
    public string id { get; set; }
    public string referenceID { get; set; }
    public List<string> AdditionalInfoList { get; set; }
}

在代码中出现的问题是为了进行序列化。虽然我获得了一个带有2个元素的列表,但没有数据,只有一些通用元数据和不是模型的一部分。

public async Task<JsonResult> OnGetIncidentsAsync()
    {
        List<IncidentModel> incidents = new List<IncidentModel>();
        using (var httpClient = new HttpClient())
        {
            using (HttpResponseMessage response = await httpClient.GetAsync("api/Incident/GetAllIncidents.json/"))
            {
                string apiResponse = await response.Content.ReadAsStringAsync();
                
                incidents = JsonConvert.DeserializeObject<List<IncidentModel>>(apiResponse);
            }
        }
        return new JsonResult(incidents);
    }

随附的图像显示了事件中的数据。 我该如何解决? “ alt =”在此处输入图像描述”>

I have an API that returns the following response as string

[
 {
    "id": "62a9f8f90346133662624bd3",
    "referenceID": "test1",
    "additionalInfoList": ["string"]
 },
 {
    "id": "62a9fba50346133662624bd4",
    "referenceID": "111",
    "additionalInfoList": ["string"]
 }
]

edit: where the exact formatting of the string is as follows with escaping backslashes:

"[{\"id\":\"62a9f8f90346133662624bd3\",\"referenceID\":\"test1\",\"additionalInfoList\":[\"string\"]},{\"id\":\"62a9fba50346133662624bd4\",\"referenceID\":\"111\",\"additionalInfoList\":[\"string\"]}]"

and the following class model

public class IncidentModel
{
    public string id { get; set; }
    public string referenceID { get; set; }
    public List<string> AdditionalInfoList { get; set; }
}

The problem arises in the code to deserialize. While I get a list with 2 elements, there is no data, only some generic metadata and fields that are not part of the model.

public async Task<JsonResult> OnGetIncidentsAsync()
    {
        List<IncidentModel> incidents = new List<IncidentModel>();
        using (var httpClient = new HttpClient())
        {
            using (HttpResponseMessage response = await httpClient.GetAsync("api/Incident/GetAllIncidents.json/"))
            {
                string apiResponse = await response.Content.ReadAsStringAsync();
                
                incidents = JsonConvert.DeserializeObject<List<IncidentModel>>(apiResponse);
            }
        }
        return new JsonResult(incidents);
    }

The attached image shows the data inside incidents.
How can I solve this?enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2025-02-14 12:47:07

因此,事实证明,我有一个名为“事件”的页面。因为我使用剃须刀页面,所以此页面具有一个型号,名为 inctissModel.cshtml.cs ,它覆盖了 InvistModel.cs 模型 forder forder 。重命名模型解决了问题。

So it turns out in Pages, I had a page named Incident. Because I use Razor pages, this page had a Model, named IncidentModel.cshtml.cs which was overriding the IncidentModel.cs from the Models folder. Renaming the model fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文