Reputation: 9960
I have a web API where if I go to a valid link, it'll display an XML.
However, in my other application, when I call:
using (var webclient = new WebClient())
{
var doc = webclient.DownloadString(url);
}
It's returning JSON data.
How do I convert this JSON to a data table? OR, should I "de-serialize" this JSON so that it becomes an XML and then I can work with the XML in order to convert it to a data table?
Upvotes: 0
Views: 7199
Reputation: 48114
You can just go directly to DataTable
with json.NET but I would recommend deserializing into other objects unless you have good reason to use a DataTable
for example if the json is actually structure like a DateTable
which isn't really normal. Json typically closely relates to native objects and in general, that's the best way to model it.
You can just use this method to deserialize it directly into a DataTable
;
DateTable myDataTable = JsonConvert.DeserializeObject<DataTable>(json);
Should you choose to go the native objects route you can just define the objects in C# like you would any other (they need properties that exactly match those in the json), then call the same method except where I have DataTable
you would instead use your type.
Upvotes: 4