为数据读取器结果动态创建JSON对象,而不管它们是什么样的? [英] Dynamically create JSON object for datareader results, regardless of what they look like?

查看:79
本文介绍了为数据读取器结果动态创建JSON对象,而不管它们是什么样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我将Newtonsoft.jsonOdbcConnection结合使用,并为我运行的每个查询手动创建对象.看起来像这样:

Right now I'm using Newtonsoft.json with my OdbcConnection and manually creating objects for each query I run. It looks something like this:

课程:

public class payload
{
    public string id;
    public string type;
    public DateTime timestmap;
    public object data;
}
public class resultPhrLastTime
{
    public string facilityId;
    public string type;
    public string time;
} 

代码:

payload result = new payload();
var resultList = new List<resultPhrLastTime>();
result.id = "someid";

//connection stuff

        while (reader.Read())
        {
            var t = new resultPhrLastTime();
            //if (verbose) log(reader[0].ToString());
            t.facilityId = reader[0].ToString();
            t.type = reader[1].ToString();
            t.time = reader[2].ToString();
            resultList.Add(t);
        }

    result.data = resultList;
    result.timestmap = DateTime.Now;
    result.type = "complex";
    string output = JsonConvert.SerializeObject(result);

这很好用,但是每次我向应用程序添加一个新查询(其中会有很多查询)时,如果数据看起来完全不同,就必须创建一个新的自定义类.

This works fine, but every time I add a new query to my app (of which there will be many) I have to create a new custom class if the data looks different at all.

我想找到一种将整个reader对象转换为JSON的方法,而不管其格式如何,因此它可能看起来像这样:

I would like to find a way to convert the entire reader object to JSON regardless of the format, so it may look like this:

SQL结果:

SQL Result:

2814814

JSON:

result: {
   timestamp: 2016-09-10 8:15,
   data: { '2814814' }
}

或者看起来像这样:

SQL结果:

SQL Result:

Apple  | 59    
Orange | 17

JSON:

result: {
   timestamp: 2016-09-10 8:15,
   data: {[ 
      'Apple':'59',
      'Orange':'17'
   ]}
}

或者可能有5列...

Or there could be 5 columns...

有没有办法做到这一点?

Is there a way to do this?

推荐答案

您可以使用c#的动态类型

You can use the dynamic type of c#

public class payload
{
    public string id;
    public string type;
    public DateTime timestmap;
    public dynamic data;
}


payload result = new payload();
var resultList = new List<Dictionary<string, dynamic>>();
result.id = "someid";

//connection stuff

while (reader.Read())
{

    var t = new Dictionary<string, dynamic>();
    for (var i = 0; i<reader.FieldCount; i++)
    {
        t[reader.GetName(i)] = reader[i];
    }
    resultList.Add(t);
}

result.data = resultList;
result.timestmap = DateTime.Now;
result.type = "complex";
string output = JsonConvert.SerializeObject(result);

动态类型由JsonConvert自动处理.

The dynamic type is handled by JsonConvert automatically.

您还可以像第一个JSON示例一样,使有效负载的数据字段成为动态字段,以处理单个字段结果.

You can also make the data field of the payload to a dynamic to handle single field results like in your first JSON example.

这篇关于为数据读取器结果动态创建JSON对象,而不管它们是什么样的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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