使用ASP.net WebAPI 2,如何返回一些自定义JSON以及Datatable结果

时间:2016-08-25 06:24:52

标签: asp.net json asp.net-web-api asp.net-web-api2

所以我一直在玩FB3和WebAPI2,它一切顺利。我已经能够从数据库中填充DataTable并且很容易返回JSON。以下是返回JSON的示例:

CURRENTLY:

[
  {
     ID: 10016,
     CREATED: "2016-08-24T21:35:45",
     MODIFIED: "2016-08-24T21:35:48",
     KEYNAME: "TEST"
  },
  {
     ID: 10017,
     CREATED: "2016-08-24T21:36:38",
     MODIFIED: "2016-08-24T21:36:41",
     KEYNAME: "TESTINT",
  }
]

但是,现在我知道这是如何完成的,我想补充一些我将在此过程中收集的其他信息。生成的JSON应如下所示:

需要:

[
   Meta: {
     status: "OK",
     message: "All's Well"
   },
   Data: {
     ID: 10016,
     CREATED: "2016-08-24T21:35:45",
     MODIFIED: "2016-08-24T21:35:48",
     KEYNAME: "TEST"
   },
   {
     ID: 10017,
     CREATED: "2016-08-24T21:36:38",
     MODIFIED: "2016-08-24T21:36:41",
     KEYNAME: "TESTINT",
    }
 ]

获取第一个JSON结果的代码如下所示:

    [Route("PerformSelectOnSettings")]
    [HttpGet]
    // GET: Connect/TestDBConnection
    public IHttpActionResult PerformSelectOnSettings()
    {

        Connection selectconnection = new Connection(fbconndetails.DBHost, string.Concat(fbconndetails.DBPath, fbconndetails.DBFile), Convert.ToInt32(fbconndetails.DBPort), fbconndetails.DBUser, fbconndetails.DBPassword, fbconndetails.DBConnectionLifeTime, fbconndetails.DBPooling, fbconndetails.DBMinPoolSize, fbconndetails.DBMaxPoolSize);
        DataTable result = new DataTable();



        string sqlcmd = "select * from settings";

        using (selectconnection.fbconnect)
        {

            try
            {
                selectconnection.fbconnect.Open();
                FbTransaction fbtrans = selectconnection.fbconnect.BeginTransaction();
                FbCommand fbcmd = new FbCommand(sqlcmd, selectconnection.fbconnect, fbtrans);

                using (FbDataReader fbsqlreader = fbcmd.ExecuteReader())
                {
                    try
                    {
                        result.Load(fbsqlreader);
                    }
                    catch (Exception e)
                    {
                        ExceptionsLogByFile logger = new ExceptionsLogByFile();
                        logger.LogException(e);
                    }
                }

            }
            catch (Exception ex)
            {
                ExceptionsLogByFile logger = new ExceptionsLogByFile();
                logger.LogException(ex);
            }
            finally
            {

            }
            selectconnection.fbconnect.Close();
            return Json(result);

        }


    }

但是,在这一点上,我真的迷失了如何将第一部分添加到JSON结果中。我做了一些研究,但我不完全确定我在寻找什么。

非常感谢任何链接/帮助!!!

1 个答案:

答案 0 :(得分:2)

预期的Json有效吗?我认为应该是这样的:

{
    "Meta": {
        "status": "OK",
        "message": "All's Well"
    },
    "Data": [{
        "ID": 10016,
        "CREATED": "2016-08-24T21:35:45",
        "MODIFIED": "2016-08-24T21:35:48",
        "KEYNAME": "TEST"
    }, {
        "ID": 10017,
        "CREATED": "2016-08-24T21:36:38",
        "MODIFIED": "2016-08-24T21:36:41",
        "KEYNAME": "TESTINT"
    }]
}

具有两个属性的Json对象 - Meta和Data,其中Data是数组。

您可以在此处验证您的json - http://jsonlint.com/

如果我正确理解您的问题,您需要将Meta详细信息添加到结果中(这是您的数据数组)。如果是这种情况,您可以选择匿名类型:

 var data = new {
                   Meta = new { Status = "OK", Message = "All's well" },
                   Data = result
                 }

return Ok(data);