简体   繁体   中英

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. I have been able to fill a DataTable from the database and return JSON pretty easily. Here's a sample of what the returning JSON looks like:

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:

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:

    [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. 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.

You can validate your json here - http://jsonlint.com/

If I understood your problem correctly, you need to add Meta details to the result (which is your data array). 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM