[英]Using ASP.net WebAPI 2, how can I return some custom JSON along with a Datatable result
So I've been playing around with FB3 and WebAPI2 and it's coming along ok. 所以我一直在玩FB3和WebAPI2,而且一切顺利。 I have been able to fill a DataTable from the database and return JSON pretty easily.
我已经能够从数据库填充DataTable并很容易地返回JSON。 Here's a sample of what the returning JSON looks like:
这是返回的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",
}
]
However, now that I know how this is done, I would like to add some other information that I would gather during the process. 但是,既然我知道如何完成此操作,那么我想添加一些在此过程中收集的其他信息。 The resulting JSON should look like the following:
生成的JSON应该如下所示:
REQUIRE: 要求:
[
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",
}
]
The code to get the first JSON result looks like the following: 获取第一个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);
}
}
At this point though, I'm really lost on how to add that first part to the JSON result. 不过,在这一点上,我真的迷失了如何将第一部分添加到JSON结果中。 I've done some researching but I'm not entirely sure what I'm looking for.
我已经做了一些研究,但是我不确定要寻找什么。
Any links / help would be greatly appreciated!!! 任何链接/帮助将不胜感激!!!
Is that Expected Json valid? 那是预期的杰森有效吗? I think it should be like:
我认为应该是这样的:
{
"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"
}]
}
A Json object with two properties - Meta and Data, where Data is array. 一个Json对象,具有两个属性-Meta和Data,其中Data是数组。
You can validate your json here - http://jsonlint.com/ 您可以在此处验证json- http://jsonlint.com/
If I understood your problem correctly, you need to add Meta details to the result (which is your data array). 如果我正确理解了您的问题,则需要将Meta详细信息添加到结果(这是您的数据数组)中。 If that is the case, you can go for anonymous type:
在这种情况下,您可以使用匿名类型:
var data = new {
Meta = new { Status = "OK", Message = "All's well" },
Data = result
}
return Ok(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.