不时地,我的系统通过Timer执行一个过程,该过程捕获由API生成的事件列表,然而,当尝试序列化时,它返回以下错误:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '[1].metadata', line 1, position 453.'
在本例中,这是我从API获得的字符串。
[{"id":"6ed69e14-6610-4b57-a06f-328f38a9e2aa","code":"PLC","fullCode":"PLACED","orderId":"c6184662-2116-4a66-9f6b-4e6caca59e0d","merchantId":"355d10e6-8825-46e3-81dc-0961bf27a5dc","createdAt":"2022-07-14T12:45:34.142Z"},{"id":"e064302e-6a65-4821-ba4c-ea7021aaf8cc","code":"CAN","fullCode":"CANCELLED","orderId":"c6184662-2116-4a66-9f6b-4e6caca59e0d","merchantId":"355d10e6-8825-46e3-81dc-0961bf27a5dc","createdAt":"2022-07-14T12:53:34.674Z","metadata":{"CANCEL_STAGE":"[PRE_CONFIRMED]","ORIGIN":"IfoodGatewayAgent","CANCEL_CODE":"902","CANCELLATION_DISPUTE":{"IS_CONTESTABLE":"CANCELLATION_IS_NOT_CONTESTABLE","REASON":"NO_CREDIT_FOR_ORDER_LIABILITIES"},"CANCELLATION_OCCURRENCE":{"tags":["NO_CREDIT_FOR_ORDER_LIABILITIES"],"RESTAURANT":{"FINANCIAL_OCCURRENCE":"NA","PAYMENT_TYPE":"NA"},"CONSUMER":{"FINANCIAL_OCCURRENCE":"NA","PAYMENT_TYPE":"NA"},"LOGISTIC":{"FINANCIAL_OCCURRENCE":"NA","PAYMENT_TYPE":"NA"}},"TIMEOUT_EVENT":false,"CANCEL_ORIGIN":"RESTAURANT","CANCEL_REASON":"AUTOMATICO - NAO CONFIRMADO PELO RESTAURANTE","CANCEL_USER":"Order BackOffice Scheduler","CANCELLATION_REQUESTED_EVENT_ID":"17da3940-661e-4d9c-a15a-57f5d1b06474"}}]
这是代码中给出错误的部分:
var data = response.Content.ReadAsStringAsync();
var bodyResponse = JsonConvert.DeserializeObject<List<Classes.OrderStatus>>(await data);
类OrderStatus:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace SAFI_Delivery.Classes
{
internal class OrderStatus
{
[JsonProperty("id")]
public string ID { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("fullCode")]
public string FullCode { get; set; }
[JsonProperty("orderId")]
public string OrderID { get; set; }
[JsonProperty("merchantId")]
public string MerchantID { get; set; }
[JsonProperty("createdAt")]
public string CreatedAt { get; set; }
[JsonProperty("metadata")]
public string Metadata { get; set; }
}
}
我想知道我是如何使它认为这是一个列表,而不是一个字符串?
你必须修复类和代码
var json= await response.Content.ReadAsStringAsync();
var bodyResponse = JsonConvert.DeserializeObject<List<OrderStatus>>(json);
类
public partial class OrderStatus
{
[JsonProperty("id")]
public Guid Id { get; set; }
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("fullCode")]
public string FullCode { get; set; }
[JsonProperty("orderId")]
public Guid OrderId { get; set; }
[JsonProperty("merchantId")]
public Guid MerchantId { get; set; }
[JsonProperty("createdAt")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("metadata", NullValueHandling = NullValueHandling.Ignore)]
public Metadata Metadata { get; set; }
}
public partial class Metadata
{
[JsonProperty("CANCEL_STAGE")]
public string CancelStage { get; set; }
[JsonProperty("ORIGIN")]
public string Origin { get; set; }
[JsonProperty("CANCEL_CODE")]
public long CancelCode { get; set; }
[JsonProperty("CANCELLATION_DISPUTE")]
public CancellationDispute CancellationDispute { get; set; }
[JsonProperty("CANCELLATION_OCCURRENCE")]
public CancellationOccurrence CancellationOccurrence { get; set; }
[JsonProperty("TIMEOUT_EVENT")]
public bool TimeoutEvent { get; set; }
[JsonProperty("CANCEL_ORIGIN")]
public string CancelOrigin { get; set; }
[JsonProperty("CANCEL_REASON")]
public string CancelReason { get; set; }
[JsonProperty("CANCEL_USER")]
public string CancelUser { get; set; }
[JsonProperty("CANCELLATION_REQUESTED_EVENT_ID")]
public Guid CancellationRequestedEventId { get; set; }
}
public partial class CancellationDispute
{
[JsonProperty("IS_CONTESTABLE")]
public string IsContestable { get; set; }
[JsonProperty("REASON")]
public string Reason { get; set; }
}
public partial class CancellationOccurrence
{
[JsonProperty("tags")]
public List<string> Tags { get; set; }
[JsonProperty("RESTAURANT")]
public Consumer Restaurant { get; set; }
[JsonProperty("CONSUMER")]
public Consumer Consumer { get; set; }
[JsonProperty("LOGISTIC")]
public Consumer Logistic { get; set; }
}
public partial class Consumer
{
[JsonProperty("FINANCIAL_OCCURRENCE")]
public string FinancialOccurrence { get; set; }
[JsonProperty("PAYMENT_TYPE")]
public string PaymentType { get; set; }
}
问题解答
谢谢,画
我不太熟悉你正在使用的JSON库,但我怀疑问题是,在你的OrderStatus类中,你有元数据属性作为字符串类型,但在你的示例JSON中,"元数据"Key有一个对象值,而不是字符串值。