1

I am converting an existing WebApi from .Net FW to .Net Core 3.1. I have a controller that receives JArray as input to my POST action method. I understand that Newtonsoft support can be continued by using Microsoft.AspNetCore.Mvc.NewtonsoftJson. But If I need to switch to the default System.Text.Json, what will be equivalent data type that I need to use from System.Text.Json to replace JArray?

Thanks!

askids
  • 1,406
  • 1
  • 15
  • 32
  • 1
    Look at the [Microsoft docs](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-5-0#how-to-search-a-jsondocument-and-jsonelement-for-sub-elements) And also [this post](https://stackoverflow.com/questions/58271901/converting-newtonsoft-code-to-system-text-json-in-net-core-3-whats-equivalent) – Karen Payne May 28 '21 at 12:14
  • 1
    Use `JsonElement`. The method [`JsonElement.EnumerateArray()`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonelement.enumeratearray?view=net-5.0#System_Text_Json_JsonElement_EnumerateArray) enumerates the array elements. There is no **type** corresponding to `JArray` so you will need to throw an exception manually if [`JsonElement.ValueKind != JsonValueKind.Array`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonelement.valuekind?view=net-5.0#System_Text_Json_JsonElement_ValueKind). – dbc May 28 '21 at 13:29
  • See [Converting newtonsoft code to System.Text.Json in .net core 3. what's equivalent of JObject.Parse and JsonProperty](https://stackoverflow.com/questions/58271901/converting-newtonsoft-code-to-system-text-json-in-net-core-3-whats-equivalent) and [Equivalent of JObject in System.Text.Json](https://stackoverflow.com/q/65620060/3744182). Do those two questions answer yours, or do you need more specific help? – dbc May 28 '21 at 13:30

0 Answers0