格式化Newtonsoft.Json.JsonConvert.SerializeObject(dataSet)的输出 [英] Formatting output of Newtonsoft.Json.JsonConvert.SerializeObject(dataSet)

查看:2237
本文介绍了格式化Newtonsoft.Json.JsonConvert.SerializeObject(dataSet)的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表.当使用

I have a DataTable. When it's serialized into JSON with

Newtonsoft.Json.JsonConvert.SerializeObject(dataTable)

我得到以下格式的结果:

I get results in the following format:

[
    {
        "CLICK_KEY": 6254523744,
        "WEB_SERVER_KEY": 291,
        "PREV_CLICK_KEY": 0,
        "NEXT_CLICK_KEY": 0,
        "SESSION_KEY": 214981151,
        *more key value pairs*
    },
    {
        "CLICK_KEY": 6254523745,
        "WEB_SERVER_KEY": 291,
        "PREV_CLICK_KEY": 0,
        "NEXT_CLICK_KEY": 0,
        "SESSION_KEY": 214746780
        *more key value pairs*
    },
    *many more objects (for each row)*
]

由于我有很多列和很多行,因此生成的JSON非常庞大.这主要是因为列名很长,并且对于每一行数据都会重复.

Since I have many columns and many rows, the resulting JSON is huge. This is mostly because column names are long and keep repeating for each row of data.

是否有一种方法可以更改Json.Net的设置,以便减小生成的JSON字符串的大小?例如,通过将输出格式设置为:

Is there a way to change settings of Json.Net so that the resulting JSON string is reduced in size? For instance, by formatting output as:

{
    "NAMES": [
        "CLICK_KEY",
        "WEB_SERVER_KEY",
        "PREV_CLICK_KEY",
        "NEXT_CLICK_KEY",
        "SESSION_KEY",
        *more keys*
    ],
    "VALUES": [
        [6254523744, 291, 0, 0, 214981151, *more values*],
        [6254523745, 291, 0, 0, 214746780, *more values*],
        *many more arrays of values (for each row)*
    ]
}

我不需要将其反序列化为表或其他对象,因此可以使用单向"解决方案.

I will not need to deserialize this back into a table or another object, so a "one-way" solution would work.

谢谢!

更新:
我遵循@spender&的建议@TravisJ并将我的DataTable转换为Newtonsoft.Json.JsonConvert.SerializeObject()输出我需要的另一种类型.请注意,我是从DataSet级别开始的,因此,如果它包含多个DataTable,它将在数组中包含每个.

Update:
I followed advice from @spender & @TravisJ and transformed my DataTable into another type for which Newtonsoft.Json.JsonConvert.SerializeObject() outputs what I need. Note how I start at DataSet level so if it contains more than one DataTable, it will include each one in the array.

var converted = from x in dataSet.Tables.Cast<DataTable>()
                select new
                {
                  NAMES = x.Columns.Cast<DataColumn>().Select(l => l.Caption),
                  VALUES = x.Rows.Cast<DataRow>().Select(l => l.ItemArray)
                };
string jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(converted);

推荐答案

您可以使用linq投影将数据表转换为所需的格式:

You could just convert the datatable to the format you need using a linq projection:

Newtonsoft.Json.JsonConvert.SerializeObject(
 dataTable.Select(
  d => new {
   Names = d.Columns.Select(c => c.ColumnName).ToArray(),
   Values = d.Rows.Select(r => r.ToArray()).ToArray()
  })
);

这篇关于格式化Newtonsoft.Json.JsonConvert.SerializeObject(dataSet)的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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