And then, after you finish everything, you'll need to convert that string back to dataset so that you can save the result into the database or display it.
In this entry I'll show you the easiest way to do that using Json.NET
In this project I use .NET 2.0. If you want to use .NET 3.5 all you have to do is remove reference Newtonsoft.Json.Net20 and add the Newtonsoft.Json.Net35 in bin folder.
Dataset to Json String
StringWriter sw = new StringWriter(); ds.WriteXml(sw, XmlWriteMode.IgnoreSchema); XmlDocument xd = new XmlDocument(); xd.LoadXml(sw.ToString()); string jsonText = JsonConvert.SerializeXmlNode(xd); txtResult.Text = jsonText;First, if you want to convert DataSet to Json String you have to convert DataSet to XMLDocument (Here I use StringWritter).
When you've already added Newtonsoft.Json.Net to you References, you can use JsonConvert to convert between JSON and XML. If you want to convert o JSON String, use SerializeXmlNode. This method takes an XmlNode and serializes it to JSON text.
Json String to Dataset
XmlDocument xd = new XmlDocument(); xd = (XmlDocument)JsonConvert.DeserializeXmlNode(txtStr.Text); DataSet ds = new DataSet(); ds.ReadXml(new XmlNodeReader(xd)); //Once you have your dataset, you can use it to save data into database or display it gvCustomers.DataSource = ds.Tables[0]; gvCustomers.DataBind();To do this, we use method DeserializeXmlNode on JsonConvert. This method takes JSON text and deserializes it into a XmlNode. And we can easily convert it into DataSet using method ReadXml.
So, I think that's enough for this entry. If you want to find out more, visit Json.NET to download the full project as well as documentations.
Hope this entry help you!
If you found any mistake or error in this entry, please let me know. I'll try to fix that a.s.a.p.
Any solution is highly appreciated!
-Share2Learn-
XmlDocument xd = new XmlDocument();
ReplyDeletexd = JsonConvert.DeserializeXmlNode(str /*str is json string*/);
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(xd));
when i execute the above code i got the following error.
Cannot add a nested relation or an element column to a table containing a SimpleContent column.
I think your xml might contain an element which has both text children and other element children.
ReplyDeleteYou can find it here: http://msdn2.microsoft.com/en-us/library/zx8h06sz.aspx
Kent
I didn't blog here anymore, but glad to know that it might help someone ^^
XmlDocument xd = new XmlDocument();
ReplyDeletexd = JsonConvert.DeserializeXmlNode(str /*str is json string*/);
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(xd));
xd = (XmlDocument)Json.........
This comment has been removed by the author.
ReplyDeleteWhen I am converting DataSet to JsonText String then some empty Columns of the table has been converted in JsonText but some of them does not converted in JsonText String .
ReplyDeletePlease give any solution of this problem .
What do you mean? Did you get any error message? Please give more details! ^^
ReplyDeleteI am getting dataset which has 8 tables, if any table has some empty column in particular row , then that empty field can not be serialized
ReplyDeletebut some of empty fields has serialized . please give me the correct solution of this problem.
ASAIK, if the column is empty, the value in the json string is empty, you can use this site to validate your returned json string: http://jsonlint.com/
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteempty columns are not serialize in json string.
DeleteIf your json string is valid, then the function is correct :)
ReplyDeleteEnjoy
sorry kent,
ReplyDeleteI have used another method in which json string is valid but in your method , empty columns are not serialize in json string.
Hi kent I have used another method to serialize dataset to json string, this json string is valid .
ReplyDeletemy question is now
How to deserialize json string to dataset ???
I already posted it in the post, please check above!
ReplyDeleteThis does not eliminate empty fields of dataset and working well........for "dataset to json string"
DeleteStringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.WriteSchema);
XmlDocument xd = new XmlDocument();
xd.LoadXml(sw.ToString());
String jsonText = JsonConvert.SerializeXmlNode(xd);
string jsonText = JsonConvert.SerializeXmlNode(xd);
txtResult.Text = jsonText;
but this has eliminate empty columns at the time of serialization , I need to all the columns in the serializataion and deserialization process.
ReplyDeleteHi Kent, hoping you can lend a hand... I am trying to convert JSON to a DataSet, using your example, but am having problems.
ReplyDeleteI'm using your JSON to DataSet conversion. The following is my JSON:
{"jsonData":[{"item1":"one"},{"item2":"two"}]}
Here's my webservice in C# code:
[WebMethod]
public string setWorkOrdersUpdated(object jsonData)
{
try
{
XmlDocument xd = new XmlDocument();
xd = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonData.ToString());
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(xd));
return "success";
}
catch (Exception e)
{
return "ERROR: " + e + "!";
}
}
Informative and nice
ReplyDelete