Json中缺少空值列 [英] Null value columns are missing in Json

查看:269
本文介绍了Json中缺少空值列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在进行投影,其中数据集应转换为Json字符串,现在我遇到了问题。根据我的要求,我用两种方法开发了Json字符串..



方法1



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



代码块



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('{'));







方法2:






Method 2 :

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);
      }











这里我在json生成后无法看到空列或空列...请让我知道通过两种方法显示空列或空列的过程...提前谢谢






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

推荐答案

使用所有列创建表并填充XML和JSON源中的值。这样你就不依赖数据载体方法进行表格布局了。



或者,不要使用提供的序列化程序,而是编写自己的会返回所有字段(但是你必须写一些带有一些值的列名(即NULL),并且在反序列化时,也要解析它。)
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).


这篇关于Json中缺少空值列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆