How to parse a JSONString To Dataset?
Your question is not very clear. I guess that what you would like to do is get back an object that could be mapped to you data set after deserializtion. Something like
DataSet myDataSet= JsonConvert.DeserializeObject<DataSet>(jsonstring)
And you keep going coding with you dataset. like accessing datatables inside the dataset.
If it's what you want to achieve and don't want to use your own POCO as suggested by previous answers. You might need to create a Typed DataSet before
Given an XML Schema that complies with the XML Schema definition language (XSD) standard, you can generate a strongly typed DataSet using the XSD.exe tool provided with the Windows Software Development Kit (SDK). More info on strongly typed Dataset
This will allow you to use the strongly typed dataset using the Deserialize method.
Bare in mind that you have to mimic your JSon Structure in the XML Schema. in order to have something compatible with your JSon Structure at the end.
As a dynamic C# solution(when you don't know the object structure to deserialize) by using @Dhaval's answer and after invaliding Deserialize<>()
method I use below method to do that:
Update: DataSet.ReadXml
has some options in reading XML node as XmlReadMode
:
private static DataSet ReadDataFromJson(string jsonString, XmlReadMode mode = XmlReadMode.Auto)
{
//// Note:Json convertor needs a json with one node as root
jsonString = $"{{ \"rootNode\": {{{jsonString.Trim().TrimStart('{').TrimEnd('}')}}} }}";
//// Now it is secure that we have always a Json with one node as root
var xd = JsonConvert.DeserializeXmlNode(jsonString);
//// DataSet is able to read from XML and return a proper DataSet
var result = new DataSet();
result.ReadXml(new XmlNodeReader(xd), mode);
return result;
}
E.g. If you want to infer a strongly typed schema from the data:
var dataset = ReadDataFromJson(yourString, XmlReadMode.InferTypedSchema);