Click here to Skip to main content
15,896,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am working on a projection where a dataset should be converted to a Json string and now I am facing a problem. Upon my requirement I have developed the Json string in two methods..

Method 1

DataSet -----> Xml ---------> Json

Code Block

XmlDocument documentSection = new XmlDocument();
                                   documentSection.LoadXml(ServiceLevelDetails.GetXml());
                                   strSectionChannelDetails.Append(JsonConvert.SerializeXmlNode(documentSection).TrimStart('{'));



Method 2 :

C#
public string GetJson(DataTable dt)
      {
          logInfo.WriteToLog("JsonChannelDetailsService", string.Format("Entered GetJson() function sucessfully."), "INFO", "GetJson()");
          System.Web.Script.Serialization.JavaScriptSerializer JSSerializer = new

          System.Web.Script.Serialization.JavaScriptSerializer();
          List<Dictionary<string, object>> DtRows =
            new List<Dictionary<string, object>>();
          Dictionary<string, object> newrow = null;

          //Code to loop each row in the datatable and add it to the dictionary object
          logInfo.WriteToLog("JsonChannelDetailsService", string.Format("Entered GetJson() function sucessfully."), "INFO", "GetJson()");
          foreach (DataRow drow in dt.Rows)
          {
              newrow = new Dictionary<string, object>();
              foreach (DataColumn col in dt.Columns)
              {
                  newrow.Add(col.ColumnName.Trim(), drow[col]);
              }
              DtRows.Add(newrow);
          }

          //Serialising the dictionary object to produce json output
          return JSSerializer.Serialize(DtRows);
      }





Here I am unable to see the Null or empty columns after json generation... Please let me know the procedure to show the null or empty columns aslo by two methods... Thanks in advance
Posted
Updated 12-Jan-15 19:35pm
v3

1 solution

Create your table with all the columns and fill the values from your XML and JSON sources. That way you don't depend on data carrier method for your table layout.

Alternatively, instead of using supplied serializer, write your own that will return all the fields (but you will have to write column names with some value (i.e. NULL) and when deserializing, parse that too).
 
Share this answer
 
Comments
jing567 13-Jan-15 2:58am    
When Program run 600 files should be procduced and each file is of 1 mb.... By using Defined functions only it takes bit time... If I use User defined function... I am prety sure that It takes more time :(
Any Alternative way?
Sinisa Hajnal 13-Jan-15 3:34am    
Yes, my first suggestion. Create the table in a function and fill the data from XML or JSON. This assumes you know what the datatable should contain.

You could also add one additional node in XML <columns><column name="x" type="y">... and an array to JSON which would define your datatable structure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900