【问题标题】:JSON deserialization error when try to reserialize restsharp response尝试重新序列化restsharp响应时出现JSON反序列化错误
【发布时间】:2022-01-08 15:17:53
【问题描述】:

我正在执行 get API 并希望从 JSON 响应中检索特定值。我想将 Job: URL 值放入一个变量中,但出现以下错误说无法反序列化 Json 对象。

我正在使用以下对象模型来映射 Json 响应。

class BlujayGetJobResponse
{
    public int TotalCount { get; set; }
    public string PreviousPage { get; set; }
    public string NextPage { get; set; }
    public List<Items> Items { get; set; }
}

Job 和 Items 类对象:

 public class Job
        {
            public string Url { get; set; }
            public string Title { get; set; }
        }
        public class Items
        {
            //public List<Consignment> Consignment { get; set; }
            public string Id { get; set; }
            public string DateCreated { get; set; }
            public string Gps { get; set; }
            public string ProcessOutcome { get; set; }
            public string ProcessOutcomeInternal { get; set; }
            public string ProcessType { get; set; }
            public string ProcessTypeInternal { get; set; }
            public string CallingCard { get; set; }
            public string AdhocLocation { get; set; }
            public string ProcessOutcomeReason { get; set; }
            public string ProcessOutcomeReasonInternal { get; set; }
            public string ProcessOutcomeReasonText { get; set; }
            public string IntendedTime { get; set; }
            public string SafePlace { get; set; }
            public string DeliveredToNeighbour { get; set; }
            public string NeighbourAddress { get; set; }
            public string IdentificationDetails { get; set; }
            public List<Job> Job { get; set; }
    
    
        }

GET API 执行的响应返回提供以下 IResponse 作为内容。当我尝试反序列化响应以检索 URL 值时,出现上述错误。我是restsharp的新手,如果有人能帮我解决这个问题,我将不胜感激。

JSON 响应:

{
  "TotalCount":2,
  "PreviousPage":null,
  "NextPage":null,
  "Items":[
    {
      "Id":"5f67fc87-4dab-4c98-bde9-8ca3ba327052",
      "DateCreated":"\/Date(1638372363000+1300)\/",
      "Gps":null,
      "ProcessOutcome":"SUC",
      "ProcessOutcomeInternal":"31",
      "ProcessType":"OFD",
      "ProcessTypeInternal":"3",
      "CallingCard":null,
      "AdhocLocation":null,
      "ProcessOutcomeReason":null,
      "ProcessOutcomeReasonInternal":null,
      "ProcessOutcomeReasonText":null,
      "IntendedTime":null,
      "SafePlace":null,
      "DeliveredToNeighbour":false,
      "NeighbourAddress":null,
      "IdentificationDetails":null,
      "Job":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Jobs/a08064c8-85fb-4e5e-8ef3-2bd24409b8d0",
        "Title":null
      },
      "Location":null,
      "Packages":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Actions/5f67fc87-4dab-4c98-bde9-8ca3ba327052/Packages/",
        "Title":null
      },
      "Signatures":null,
      "User":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Users/126",
        "Title":null
      },
      "Link":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Actions/5f67fc87-4dab-4c98-bde9-8ca3ba327052",
        "Title":null
      },
      "TransactionId":null
    },
    {
      "Id":"33b0c532-3ca5-4eee-a1db-d012cae064ea",
      "DateCreated":"\/Date(1638419167000+1300)\/",
      "Gps":null,
      "ProcessOutcome":"DELCRE",
      "ProcessOutcomeInternal":"330",
      "ProcessType":"DEL",
      "ProcessTypeInternal":"1",
      "CallingCard":null,
      "AdhocLocation":null,
      "ProcessOutcomeReason":null,
      "ProcessOutcomeReasonInternal":null,
      "ProcessOutcomeReasonText":null,
      "IntendedTime":null,
      "SafePlace":null,
      "DeliveredToNeighbour":false,
      "NeighbourAddress":null,
      "IdentificationDetails":null,
      "Job":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Jobs/d9b93ae1-2b37-400b-ad43-978bbad024d9",
        "Title":null
      },
      "Location":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Depots/53",
        "Title":null
      },
      "Packages":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Actions/33b0c532-3ca5-4eee-a1db-d012cae064ea/Packages/",
        "Title":null
      },
      "Signatures":null,
      "User":null,
      "Link":{
        "Url":"https://freightways-uat.mobilestar.blujaysolutions.net:444/API/v2/Actions/33b0c532-3ca5-4eee-a1db-d012cae064ea",
        "Title":null
      },
      "TransactionId":null
    }
  ]
}

这是 Postman 响应的样子:

如果你能帮我弄清楚,不胜感激。

非常感谢您的帮助。

谢谢。

【问题讨论】:

  • 请勿发布代码图片。相反,将代码复制并粘贴到您的问题文本中,然后单击代码格式按钮,如下所示:{ }
  • 对不起,@GregBurghardt 对不起。我已经编辑了帖子添加代码以替换屏幕截图。
  • 还尝试添加更多上下文来解释我的问题。我对 Restsharp 很陌生,目前正在尝试在理解现有代码库的同时处理它。抱歉我无法清楚地解释这个问题。谢谢。
  • 我格式化了 JSON 响应。您可以在任何搜索引擎中搜索“format json”,并且有很多免费网站可以让您快速格式化 JSON 以提高可读性。

标签: json api restsharp specflow json-api-response-converter


【解决方案1】:

我找到了解决此问题的方法。 Job 元素是一个对象而不是一个列表。于是改变对象模型如下,错误就解决了。

public class Job
{
    public string Url { get; set; }
    public object Title { get; set; }
}

【讨论】:

  • 难道Title 属性也需要是string 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多