question

Andrus-0543 avatar image
0 Votes"
Andrus-0543 asked AgaveJoe answered

Deserialize nested json

Deeserializing json in ASP.NET 5 MVC application using


 var tulemus = JsonSerializer.Deserialize<EstoJarelMaksTulemnus>(apiResponse, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
    
    
     public class EstoJarelMaksTulemnus
     {
         public string[] Errors { get; set; }
         public EstoData Data { get; set; }
         public string Mac { get; set; }
     }
    
     public class EstoData
     {
         public string Id { get; set; }
         public string Status { get; set; }
         public string Purchase_url { get; set; }
         public string Merchant_reference { get; set; }
         public decimal Amount { get; set; }
         public string Currency { get; set; }
         public bool Is_test { get; set; }
         public string Return_url { get; set; }
         public string Notification_url { get; set; }
     }

throws error

System.Text.Json.JsonException: The JSON value could not be converted
to Store.Controllers.CheckoutController+EstoData. Path: $.data |
LineNumber: 0 | BytePositionInLine: 497. at
System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type
propertyType) ...

apiResponse contains single line string:

 {"errors":[],"data":"{\"id\":\"iUW3YiDIz5Ahg5eO8hV7d3Cv7SVbZ913\",\"status\":\"CREATED\",\"purchase_url\":\"https:\\\/\\\/user.esto.ee\\\/application\\\/iUW3YiDIz5Ahg5eO8hV7d3Cv7SVbZ913\",\"merchant_reference\":\"158502\",\"amount\":93.95,\"currency\":\"EUR\",\"is_test\":true,\"return_url\":\"http:\\\/\\\/localhost:54274\\\/CheckoutController\\\/EstoJarelmaksOK?tellimus=104742\",\"notification_url\":\"http:\\\/\\\/localhost:54274\\\/CheckoutController\\\/EstoJarelmaksTeade?tellimus=104742\"}","mac":"E9C3E61FC347D80200F542C43ABDCF464D537C37A609EC878F95B5F526271A2287F2D2E507B5A14FA3AF5F7A6D4CDECB6E8A1DBDF9A5633E0B3AD96DA35FA1C9"}


postion 497 seems pointt to end of Data property before "mac" property.

How to deserialize this json ?

dotnet-aspnet-core-mvcdotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

It looks like you are dealing with a generic response object which you must deserialize first. Then you can deserialize data string.

     class Program
     {
         static async Task Main(string[] args)
         {
             byte[] fileContents = await File.ReadAllBytesAsync("json1.json");
             EstoJarelMaksTulemnus response = JsonSerializer.Deserialize<EstoJarelMaksTulemnus>(fileContents);
    
             EstoData data = JsonSerializer.Deserialize<EstoData>(response.data);
             Console.WriteLine($"Id: {data.id}");
         }
     }
     public class EstoJarelMaksTulemnus
     {
         public object[] errors { get; set; }
         public string data { get; set; }
         public string mac { get; set; }
     }
     public class EstoData
     {
         public string id { get; set; }
         public string status { get; set; }
         public string purchase_url { get; set; }
         public string merchant_reference { get; set; }
         public float amount { get; set; }
         public string currency { get; set; }
         public bool is_test { get; set; }
         public string return_url { get; set; }
         public string notification_url { get; set; }
     }



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.