Home > Software design >  How to deserialize a string containing multiple JSON
How to deserialize a string containing multiple JSON

Time:07-15

From time to time, my system, through a Timer, executes a procedure that captures a list of events generated by the API, however, when trying to serialize, it returns the following error.

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '[1].metadata', line 1, position 453.'

In this case, this is the string I get from the 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"}}]

This is the part that gives an error in the code:

var data = response.Content.ReadAsStringAsync();
var bodyResponse = JsonConvert.DeserializeObject<List<Classes.OrderStatus>>(await data);

Class 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; }
    }
}

I would like to know how I make it consider that this is a list and not a string?

CodePudding user response:

you have to fix classes and code

var json= await response.Content.ReadAsStringAsync();

var bodyResponse = JsonConvert.DeserializeObject<List<OrderStatus>>(json);

classes

    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; }
    }

CodePudding user response:

Answered Question.

Thanks, Drew

I'm not too familiar with the JSON library you're using, but I suspect the issue is that, in your OrderStatus class, you have the Metadata property as a string type, but in your example JSON, the "metadata" key has an object value, not a string value.

  • Related