question

Khushisshaikh avatar image
0 Votes"
Khushisshaikh asked Khushisshaikh commented

Does System.Text.Json Deserialize into DataTable?

Hello .Net Gurus - I have checked MS Documentation and I couldn't find any example of serialize json data using System.Text.JSON into DataTable. I have .Net Framwork 4.7.2 and did install the System.Text.Json NuGet package. The desrialization is working perfectly using NewtonSoft's JSON. I prefer to use Microsoft's libraries. Any guidance is much appreciated.

 //Calling Code
    
  dt = JsonSerializer.Deserialize<DataTable>(apiResponse, new JsonSerializerOptions
              {
                  PropertyNameCaseInsensitive = true
              });

PS: While goggling I came across the post where a comment was "System.Text.JSON" doesn't support deserialize into DataTable. Is this true?


Thanks & Regards,

Khushi

windows-forms
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

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered Khushisshaikh commented

Hi Khushisshaikh,
I did some research about System.Text.Json and found that System.Text.Json has more restrictions.
For example, there is currently no built-in support for types like DataSet and DataTable in System.Text.Json.
And the official document says, there may be solutions for unsupported functions. The solution is to customize converters, they may not provide exactly the same functions as Newtonsoft.Json.
More details you can refer to this document.
So if you want to deserialize json to datatable, Newtonsoft.Json is a good choice.
Here is a code example you can refer to.17589-example.txt
Best Regards,
Daniel Zhang



example.txt (1.1 KiB)
· 1
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.

Thank you Daniel! Really appreciate for the detailed and supported information.

0 Votes 0 ·