.NET CORE:避难所化问题+如何更好地了解无用的错误文本

发布于 2025-02-13 17:30:55 字数 4978 浏览 3 评论 0原文

我有以下JSON,我通过验证器和“将JSON粘贴为类粘贴”来构建Visual Studio中的模型:

{
  "entity": {
    "Successful Requests": [
      {
        "Status": 201,
        "Resource ID": 34715818,
        "Message": "Created resource 1 at URI: /rest/equipment/ids/34715818"
      },
      {
        "Status": 201,
        "Resource ID": 34715838,
        "Message": "Created resource 2 at URI: /rest/equipment/ids/34715838"
      }
    ],
    "Failed Requests": [
      {
        "Status": 500,
        "Resource ID": -1,
        "Message": "Failed to create new resource 1. Bulk update failed. Cause: Template 'xxx' not found."
      },
      {
        "Status": 500,
        "Resource ID": -1,
        "Message": "Failed to create new resource 2. Bulk update failed. Cause: Template 'xxx' not found."
      }
    ]
  },
  "variant": {
    "language": null,
    "mediaType": {
      "type": "application",
      "subtype": "json",
      "parameters": {},
      "wildcardType": false,
      "wildcardSubtype": false
    },
    "encoding": null,
    "languageString": null
  },
  "annotations": [],
  "mediaType": {
    "type": "application",
    "subtype": "json",
    "parameters": {},
    "wildcardType": false,
    "wildcardSubtype": false
  },
  "language": null,
  "encoding": null
}

这构建了以下类(我在Entity类中添加了JSONProperties因为属性名称包含空格,所以我也尝试了没有它们的空间,并且会遇到相同的错误):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace xxx.Data.Models
{
    public class Rootobject
    {
        public Entity entity { get; set; }

        public Variant variant { get; set; }
        public object[] annotations { get; set; }
        public Mediatype1 mediaType { get; set; }
        public object language { get; set; }
        public object encoding { get; set; }
    }

    public class Entity
    {
        [JsonProperty("Successful Requests")]
        public SuccessfulRequest[] SuccessfulRequests { get; set; }
        [JsonProperty("Failed Requests")]
        public FailedRequest[] FailedRequests { get; set; }
    }

    public class SuccessfulRequest
    {
        public int Status { get; set; }
        public int ResourceID { get; set; }
        public string Message { get; set; }
    }

    public class FailedRequest
    {
        public int Status { get; set; }
        public int ResourceID { get; set; }
        public string Message { get; set; }
    }

    public class Variant
    {
        public object language { get; set; }
        public Mediatype mediaType { get; set; }
        public object encoding { get; set; }
        public object languageString { get; set; }
    }

    public class Mediatype
    {
        public string type { get; set; }
        public string subtype { get; set; }
        public Parameters parameters { get; set; }
        public bool wildcardType { get; set; }
        public bool wildcardSubtype { get; set; }
    }

    public class Parameters
    {
    }

    public class Mediatype1
    {
        public string type { get; set; }
        public string subtype { get; set; }
        public Parameters1 parameters { get; set; }
        public bool wildcardType { get; set; }
        public bool wildcardSubtype { get; set; }
    }

    public class Parameters1
    {
    }
}

我在代码中进行API调用:

var response = await _repositorySvc.PutBulkEquipment(requestBody, Guid.NewGuid());
var result = response.Content.ReadAsStringAsync().Result;
_logger.LogWarning("------------------Result:");
_logger.LogWarning(result);

它在控制台中返回以下JSON字符串:

{
  "entity": "{\"Successful Requests\":[{\"Status\":201,\"Resource ID\":34715872,\"Message\":\"Created resource 1 at URI: /rest/equipment/ids/34715872\"},{\"Status\":201,\"Resource ID\":34715892,\"Message\":\"Created resource 2 at URI: /rest/equipment/ids/34715892\"}],\"Failed Requests\":[]}",
  "variant": {
    "language": null,
    "mediaType": {
      "type": "application",
      "subtype": "json",
      "parameters": {},
      "wildcardType": false,
      "wildcardSubtype": false
    },
    "encoding": null,
    "languageString": null
  },
  "annotations": [],
  "mediaType": {
    "type": "application",
    "subtype": "json",
    "parameters": {},
    "wildcardType": false,
    "wildcardSubtype": false
  },
  "language": null,
  "encoding": null
}

然后,当我尝试进行挑选时字符串到实体类:

var deserializedResponse = JsonConvert.DeserializeObject<Rootobject>(result);

我会收到以下错误:

错误转换值 “ {“成功请求”:[{“ status”:201,“资源ID”:34715872,“消息”:“创建资源1在uri:/rest/repucation/decubl/ids/34715872”},{ ,“资源ID”:34715892,“消息”:“在URI上创建了资源2: /REST/设备/IDS/34715892“}],“失败的请求”:[]}“
键入'xxx.data.models.entity'。
路径“实体”,第1行,位置290。

  1. 。一天半?一切对我来说都是正确的,我很难过 - 我做了很多挑战,通常是EZ模式,所以我不确定我缺少什么。

  2. 是否有任何方法可以更好地了解.NET Core中的避免化问题?

谢谢你!

I have the following JSON which I ran through a validator and 'Pasted JSON as classes' to build a model from the schema in Visual Studio:

{
  "entity": {
    "Successful Requests": [
      {
        "Status": 201,
        "Resource ID": 34715818,
        "Message": "Created resource 1 at URI: /rest/equipment/ids/34715818"
      },
      {
        "Status": 201,
        "Resource ID": 34715838,
        "Message": "Created resource 2 at URI: /rest/equipment/ids/34715838"
      }
    ],
    "Failed Requests": [
      {
        "Status": 500,
        "Resource ID": -1,
        "Message": "Failed to create new resource 1. Bulk update failed. Cause: Template 'xxx' not found."
      },
      {
        "Status": 500,
        "Resource ID": -1,
        "Message": "Failed to create new resource 2. Bulk update failed. Cause: Template 'xxx' not found."
      }
    ]
  },
  "variant": {
    "language": null,
    "mediaType": {
      "type": "application",
      "subtype": "json",
      "parameters": {},
      "wildcardType": false,
      "wildcardSubtype": false
    },
    "encoding": null,
    "languageString": null
  },
  "annotations": [],
  "mediaType": {
    "type": "application",
    "subtype": "json",
    "parameters": {},
    "wildcardType": false,
    "wildcardSubtype": false
  },
  "language": null,
  "encoding": null
}

This builds the following classes (I added the JsonProperties in the entity class because the property names contain spaces, I also tried without them and I get the same error):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace xxx.Data.Models
{
    public class Rootobject
    {
        public Entity entity { get; set; }

        public Variant variant { get; set; }
        public object[] annotations { get; set; }
        public Mediatype1 mediaType { get; set; }
        public object language { get; set; }
        public object encoding { get; set; }
    }

    public class Entity
    {
        [JsonProperty("Successful Requests")]
        public SuccessfulRequest[] SuccessfulRequests { get; set; }
        [JsonProperty("Failed Requests")]
        public FailedRequest[] FailedRequests { get; set; }
    }

    public class SuccessfulRequest
    {
        public int Status { get; set; }
        public int ResourceID { get; set; }
        public string Message { get; set; }
    }

    public class FailedRequest
    {
        public int Status { get; set; }
        public int ResourceID { get; set; }
        public string Message { get; set; }
    }

    public class Variant
    {
        public object language { get; set; }
        public Mediatype mediaType { get; set; }
        public object encoding { get; set; }
        public object languageString { get; set; }
    }

    public class Mediatype
    {
        public string type { get; set; }
        public string subtype { get; set; }
        public Parameters parameters { get; set; }
        public bool wildcardType { get; set; }
        public bool wildcardSubtype { get; set; }
    }

    public class Parameters
    {
    }

    public class Mediatype1
    {
        public string type { get; set; }
        public string subtype { get; set; }
        public Parameters1 parameters { get; set; }
        public bool wildcardType { get; set; }
        public bool wildcardSubtype { get; set; }
    }

    public class Parameters1
    {
    }
}

I make an API call in code:

var response = await _repositorySvc.PutBulkEquipment(requestBody, Guid.NewGuid());
var result = response.Content.ReadAsStringAsync().Result;
_logger.LogWarning("------------------Result:");
_logger.LogWarning(result);

Which returns the following JSON string in the console:

{
  "entity": "{\"Successful Requests\":[{\"Status\":201,\"Resource ID\":34715872,\"Message\":\"Created resource 1 at URI: /rest/equipment/ids/34715872\"},{\"Status\":201,\"Resource ID\":34715892,\"Message\":\"Created resource 2 at URI: /rest/equipment/ids/34715892\"}],\"Failed Requests\":[]}",
  "variant": {
    "language": null,
    "mediaType": {
      "type": "application",
      "subtype": "json",
      "parameters": {},
      "wildcardType": false,
      "wildcardSubtype": false
    },
    "encoding": null,
    "languageString": null
  },
  "annotations": [],
  "mediaType": {
    "type": "application",
    "subtype": "json",
    "parameters": {},
    "wildcardType": false,
    "wildcardSubtype": false
  },
  "language": null,
  "encoding": null
}

Then, when I attempt to deserialize the string to the Entity class:

var deserializedResponse = JsonConvert.DeserializeObject<Rootobject>(result);

I get the following error:

Error converting value
"{"Successful Requests":[{"Status":201,"Resource ID":34715872,"Message":"Created resource 1 at URI: /rest/equipment/ids/34715872"},{"Status":201,"Resource ID":34715892,"Message":"Created resource 2 at URI: /rest/equipment/ids/34715892"}],"Failed Requests":[]}"
to type 'xxx.Data.Models.Entity'.
Path 'entity', line 1, position 290.

  1. Would anyone be able to spot any mistakes I'm making that I might be missing in the above scenario that could be contributing to me banging my head on a wall for the past day and a half on this? Everything looks correct to me and I'm stumped - I do a lot of deserialization and usually it's ez-mode so I'm not sure what I'm missing.

  2. Is there any way to get better insight into deserialization issues in .NET Core?

Thank you!

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

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

发布评论

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

评论(1

孤单情人 2025-02-20 17:30:55

如果这是JSON响应:

"entity": "{\"Successful Requests\":[...],\"Failed Requests\":[]}",

那么实体是类型字符串。请注意开始和结束时的引号。另请注意,所有内部引号均使用后斜切逃脱。

错误消息完全说明,它不能将该字符串转换为对象。它确实期望的东西更像

"entity": {"Successful Requests":[...], "Failed Requests":[]},

If this is the JSON response:

"entity": "{\"Successful Requests\":[...],\"Failed Requests\":[]}",

then entity is of type string. Note the quotes in the beginning and the end. Also note that all the inner quotes are escaped using backslashes.

And the error message says exactly that, it cannot convert that string into an object. it did expect something more like

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