将Datatable转换为JSON,不含表头[英] Convert Datatable to JSON without table header

本文是小编为大家收集整理的关于将Datatable转换为JSON,不含表头的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我正在尝试将数据转换为JSON.我可以得到它,但问题是我得到的属性名称对应于表格的序列化结果字符串:

private string TestForEach(DataTable table)
    {
        var categoryList = new List<FilterCountry>(table.Rows.Count);
        foreach (DataRow row in table.Rows)
        {
            var values = row.ItemArray;
            var category = new FilterCountry()
            {
                Country = values[0].ToString(),
                Count = (Int32)values[1]
            };
            categoryList.Add(category);
        }
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return serializer.Serialize(categoryList);
    }

这返回一个字符串:

[{"Count":2,"Country":"China"},{"Count":3,"Country":"India"},{"Count":3,"Country":"Pakistan"},{"Count":5,"Country":"United Arab Emirates"}]

但是我不需要字符串中的属性名称,我只需要:

"China":2,"India":3,"Pakistan":3,"United Arab Emirates":5

推荐答案

您需要实现和使用自定义JavascriptConverter .在您的情况下,看起来可能是这样:

public class FilterCountryListConverter : JavaScriptConverter
{
   public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
   {
       throw new NotImplementedException();
   }

   public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
   {
       var output = new Dictionary<string, object>();
       List<FilterCountry> filters = obj as List<FilterCountry>;
       filters.ForEach( f => output.Add( f.Country, f.Count ) );
       return output;
   }

   public override IEnumerable<Type> SupportedTypes
   {
       get { return new List<Type>(new List<Type>(new Type[] { typeof(List<FilterCountry>) }));  }
   }
}

然后,在调用序列化例程之前,您需要在序列化器中注册它:

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new FilterCountryListConverter() });
var res = serializer.Serialize(categoryList);

然后,您将获得如下:

的输出

{"China":2,"India":2,"Pakistan":3,"United Arab Emirates":5}

本文地址:https://www.itbaoku.cn/post/1511318.html

问题描述

I'm trying to convert a datatable to JSON. I can get it but the problem is I'm getting property names corresponding to header of the table in serialization result string:

private string TestForEach(DataTable table)
    {
        var categoryList = new List<FilterCountry>(table.Rows.Count);
        foreach (DataRow row in table.Rows)
        {
            var values = row.ItemArray;
            var category = new FilterCountry()
            {
                Country = values[0].ToString(),
                Count = (Int32)values[1]
            };
            categoryList.Add(category);
        }
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return serializer.Serialize(categoryList);
    }

This returns a string:

[{"Count":2,"Country":"China"},{"Count":3,"Country":"India"},{"Count":3,"Country":"Pakistan"},{"Count":5,"Country":"United Arab Emirates"}]

but I don't need property names in the string, I just need :

"China":2,"India":3,"Pakistan":3,"United Arab Emirates":5

推荐答案

You need to implement and use custom JavaScriptConverter. In your case it could look like this:

public class FilterCountryListConverter : JavaScriptConverter
{
   public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
   {
       throw new NotImplementedException();
   }

   public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
   {
       var output = new Dictionary<string, object>();
       List<FilterCountry> filters = obj as List<FilterCountry>;
       filters.ForEach( f => output.Add( f.Country, f.Count ) );
       return output;
   }

   public override IEnumerable<Type> SupportedTypes
   {
       get { return new List<Type>(new List<Type>(new Type[] { typeof(List<FilterCountry>) }));  }
   }
}

Then you need to register it in your serializer, before you call serialization routine:

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new FilterCountryListConverter() });
var res = serializer.Serialize(categoryList);

Then you will get output like below:

{"China":2,"India":2,"Pakistan":3,"United Arab Emirates":5}